Skip to main content
A record is an anonymous, immutable, aggregate type in Dart that allows bundling multiple objects into a single value. Records are fixed-size, heterogeneous, and structurally typed, meaning their type signature is determined entirely by the types, order, and names of their fields rather than a formal class declaration.

Syntax and Initialization

Records are instantiated using a comma-separated list of expressions enclosed in parentheses. They support both positional and named fields. A record with exactly one positional field requires a trailing comma in its instantiation. Without the trailing comma, Dart evaluates the construct as a standard parenthesized expression rather than a record.

Type Annotations

A record’s type is a structural combination of its field types. Positional fields are enclosed in parentheses, while named fields are enclosed in curly braces {} within the parentheses. Just like instantiation, a single-element positional record type requires a trailing comma.

Field Access

Fields are accessed using dot notation. Named fields are accessed via their declared identifier. Positional fields are accessed using a $ prefix followed by their 1-based index. Positional indexes skip named fields.

Structural Equality

Records implement structural equality. Two records are considered equal (==) if they have the exact same “shape” (the same set of positional field types and named fields) and their corresponding field values are equal. For named fields, the order of declaration does not affect the shape, type, or equality. For positional fields, order is strictly enforced.

Destructuring

Records integrate directly with Dart’s pattern matching, allowing their fields to be unpacked into distinct local variables in a single statement.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More