Syntax Structure
A record pattern consists of a parenthesized list of subpatterns representing positional and named fields. Crucial Distinction: To distinguish a record pattern containing exactly one positional field from a parenthesized subpattern (which merely groups a pattern), a trailing comma is mandatory. Without the comma, the compiler treats the syntax as a parenthesized expression rather than a record pattern. Named fields do not require a trailing comma for disambiguation.Positional Field Matching
Positional subpatterns match the subject record’s fields based strictly on ordinal position. The matching is exhaustive regarding arity: the number of positional subpatterns in the pattern must exactly match the number of positional fields in the subject record type. If the static type of the subject record is known, a mismatch in the number of positional fields results in a compile-time error.Named Field Matching
Named subpatterns match based on the field key, independent of their physical order in the pattern. Unlike positional matching, named field matching is not exhaustive; a record pattern does not need to match every named field present in the subject record. Fields present in the subject but omitted from the pattern are ignored.Variable Binding Shorthand
When binding a named field to a variable of the same name, the explicit field key can be omitted by prefixing the variable name with a colon. The syntax:name is syntactic sugar for name: name.
The variable created by this shorthand infers its storage semantics (e.g., var, final) from the enclosing declaration context.
Type Annotation and Checking
Record patterns can assert specific types for individual fields by using variable patterns with type annotations or object patterns. If the runtime type of a field does not match the annotated type in the subpattern, the entire record match fails.Wildcards
The wildcard pattern (_) matches any value without binding it to a variable. In record patterns, wildcards are frequently used to satisfy the strict positional arity requirement while discarding specific field values.
Recursive Composition
Record patterns are composable; a subpattern within a record pattern can be any valid pattern, including another record pattern, a list pattern, or an object pattern. This allows for deep destructuring of nested data structures.Master Dart with Deep Grasping Methodology!Learn More





