Classes, Enums, Extensions
Enums and Exhaustive Switch
An enum lists fixed values. A switch expression over the enum is checked for exhaustiveness.
Program
Play the program to label a priority with an exhaustive switch.
enums.dart
enum Priority { low, medium, high }
void main() {
var p = Priority.high;
var label = switch (p) {
Priority.high => 'urgent',
Priority.medium => 'normal',
Priority.low => 'whenever',
};
print(label);
}
enum
`enum Priority { ... }` defines fixed named values.
Priority.high
Each value is accessed as `EnumName.value`.
exhaustive
Listing every variant lets the compiler skip a `default` arm.