switch Over Enums Exhaustively
Handle every case the compiler checks.
switch Loves Enums
When a value is one of a few choices, switch handles each case cleanly. Enums and switch are a natural pair.
A Basic switch
List one case per enum member and the code to run for it. Dart matches the value to its case and runs that branch.
switch (status) {
case Status.pending: print("Waiting");
}All lessons in this course
- Declaring and Using Plain Enums
- Enum Values, index, and name
- Enhanced Enums With Fields and Methods
- switch Over Enums Exhaustively