What are Dart 3 Records and how does pattern matching work?
TL;DR: Dart 3 Records are anonymous, immutable value types that group multiple values. Pattern matching in switch expressions lets you destructure, match, and bind values with exhaustiveness checking.
Full Answer
Records
Records are fixed-size, heterogeneous collections like tuples. They're value types — two records with the same fields and values are equal. Great for returning multiple values from a function without defining a class.
Pattern Matching
Dart 3's switch expressions + patterns let you match on type, structure, and values simultaneously. The compiler checks exhaustiveness for sealed classes and enums.
Patterns work in variable declarations, for loops, if statements, and switch expressions. They eliminate much of the verbosity of isA/as casting patterns.
Code Examples
Alice is 30 Bob is 25 three-element: 1, 2, 3
Interview Tip
Records are value types — (1, 'a') == (1, 'a') is true without needing to override ==. Combined with sealed classes and exhaustive switch, they enable algebraic data types in Dart.