Control Flow
Switch Expression
Pattern Arms
Dart 3 introduces switch expressions with patterns. Each arm yields a value, and _ is the wildcard.
Program
Play the program to map an integer code to a label.
switch_pattern.dart
void main() {
Object code = 2;
var label = switch (code) {
1 => 'one',
2 => 'two',
_ => 'many',
};
print(label);
}
switch expression
`switch (x) { ... }` evaluates to a value.
pattern arm
`1 =>`, `2 =>`, etc. match constant patterns.
wildcard
`_` matches anything not handled above.