What are Records?
Introduced in Dart 3.0, records are anonymous, immutable data structures that let you bundle multiple values together without declaring a full class. They are similar to tuples in other languages. Records can have both positional fields (like in a tuple) and named fields. They are perfect for returning multiple values from a function or grouping related data temporarily.
Syntax of Records
Records are written using parentheses () containing a comma‑separated list of expressions. The type of a record is inferred, but you can also annotate it explicitly. There are two styles: positional fields (ordered) and named fields (named).
Creating Records
You can create records using literal syntax. The type is automatically inferred. If you need to specify the type explicitly (e.g., for a variable declaration or function return), you can use the record type syntax: (int, String) for positional, or ({int age, String name}) for named. Named fields are enclosed in curly braces.
Accessing Record Fields
Fields in a record are accessed differently depending on whether they are positional or named:
- Positional fields are accessed using the
$operator followed by the field index (starting at 1). For example,record.$1is the first field.
- Positional fields are accessed using the
- Named fields are accessed using dot notation (e.g.,
record.name).
- Named fields are accessed using dot notation (e.g.,
Record Types and Type Safety
Records are structural types. Two records are the same type if they have the same shape (same number of fields, same field types, same named field names). The field names matter for named fields; for positional fields, only the order matters.
Pattern Matching with Records
One of the most powerful features of records is using them with pattern matching (also introduced in Dart 3.0). You can destructure records in variable declarations, switch statements, and more.
Pattern matching also works in switch statements, allowing you to match and destructure records in a single go.
Records vs Classes
- Records are lightweight, anonymous, and immutable. They are best for temporary grouping of data or returning multiple values from a function.
- Classes provide names, methods, private fields, and can be extended. Use classes when you need behaviour, encapsulation, or identity beyond the data.
When to Use Records
- Returning multiple values from a function without creating a wrapper class.
- Lightweight grouping of related data that is only used locally.
- As keys in maps (records are value‑based, so two records with same content are equal).
- In pattern matching for concise data extraction.
Key Takeaways
- Records are anonymous, immutable data structures introduced in Dart 3.0.
- They can have positional fields (accessed with
$1,$2, …) and named fields (accessed with dot).
- They can have positional fields (accessed with
- Record types are structural; two records with the same shape are compatible.
- Records shine when used with pattern matching (destructuring).
- Use records for temporary data grouping; use classes for full‑fledged objects.