Sub topic 12
Records, Patterns, and Switch Expressions
This chapter assumes basic Dart syntax, classes, collections, and functions. You should also understand that Dart is statically typed and supports sound null safety.
Records: small, fixed data groups
A record is an immutable value that groups several values without defining a class. Records are useful when a function needs to return more than one related result, but the result does not need a full data type.
A positional record stores values by position:
final point = (10, 20);
print(point.$1);
print(point.$2);A named record stores values with field names:
final user = (name: 'Ada', age: 36);
print(user.name);
print(user.age);Dart infers the record's type. You can also write the type explicitly:
(String, int) coordinate = (10, 20);
({String name, int age}) person = (name: 'Ada', age: 36);Records use structural equality. Two records with the same shape and equal values compare as equal.
final first = (name: 'Ada', age: 36);
final second = (name: 'Ada', age: 36);
print(first == second); // truePatterns: matching and extracting values
A pattern is a rule that checks a value and can extract parts of it. Patterns work especially well with records, objects, lists, and maps.
Destructuring assigns record fields to local variables:
final (x, y) = (3, 4);
print(x + y); // 7
final (:name, :age) = (name: 'Ada', age: 36);
print('$name is $age');The :name syntax means “read the named field called name and create a local variable with that name.”
Patterns can also appear in if statements:
final value = (name: 'Ada', age: 36);
if (value case (name: final name, age: >= 18)) {
print('$name is an adult');
}This condition matches only a record whose age is at least 18.
Switch expressions and exhaustive matching
A switch expression produces a value. Unlike a traditional switch statement, it can appear directly on the right side of an assignment or after return.
A sealed class hierarchy lets Dart check that every possible subtype is handled:
sealed class Shape {}
class Circle extends Shape {
final double r;
Circle(this.r);
}
class Square extends Shape {
final double side;
Square(this.side);
}
double area(Shape shape) => switch (shape) {
Circle(:var r) => 3.14159 * r * r,
Square(:var side) => side * side,
};
void main() {
final person = (name: 'Ada', age: 36);
final (x, y) = (1, 2);
print('${person.name} ${person.age} $x $y');
print(area(Square(3)));
}Circle(:var r) is an object pattern. It checks the object's type and extracts its r field. Because Shape is sealed, the switch must cover both Circle and Square.
Common errors and trade-offs
- A record is best for a small, local group of values. Use a class when the values have meaningful behavior, documentation, or a stable public API.
- Positional records are compact, but named records are clearer when fields have different meanings.
- A switch expression must be exhaustive. Adding a wildcard such as
_can hide missing cases, so use one only when a fallback is intentional. - Pattern variables exist only in the scope where the pattern matched.
- A pattern checks structure and values. It does not automatically convert an incompatible value to another type.
Exercise
When should you choose a named record instead of a class?
Show answer
Choose a named record for a small, immutable group of values that needs no methods or long-term public API. Choose a class when the data has behavior, invariants, or a meaningful reusable type.