Overview
Switch statements provide a clean way to handle multiple conditions based on the value of an expression. They are particularly useful when you need to compare a single value against multiple possible matches.
Basic Syntax
switch (expression) {
case value1:
// Code to execute
break;
case value2:
// Code to execute
break;
default:
// Code to execute if no case matches
}
Each case must end with break, continue, return, or throw. Otherwise, execution will fall through to the next case.
String Matching Example
Switch statements work well with strings:
void main() {
String dia = 'sábado';
switch (dia) {
case 'lunes':
print('Inicio de Semana');
break;
case 'sábado':
break;
case 'viernes':
print('Casi es fin de semana');
break;
case 'domingo':
print('Fin de Semana');
break;
default:
print('Día inválido!');
}
}
Integer Matching Example
Switch statements can also match integer values:
int calificacion = 8;
switch (calificacion) {
case 6:
print('Suficiente');
break;
case 7:
print('Bueno');
break;
case 8:
print('Muy Bueno');
break;
case 9:
print('Sobresaliente');
break;
case 10:
print('Excelente');
break;
default:
print('No Suficiente');
}
// Output: Muy Bueno
Pattern Matching (Dart 3.0+)
Dart 3.0 introduced enhanced pattern matching in switch statements, allowing for more complex conditions.
int calificacion = 0;
switch (calificacion) {
case >= 0 && <= 5:
print('No Suficiente');
break;
case 6:
print('Suficiente');
break;
case 7:
print('Bueno');
break;
case 8:
print('Muy Bueno');
break;
case 9:
print('Sobresaliente');
break;
case 10:
print('Excelente');
break;
}
Complete Example
void main() {
String dia = 'sábado';
switch (dia) {
case 'lunes':
print('Inicio de Semana');
break;
case 'sábado':
break;
case 'viernes':
print('Casi es fin de semana');
break;
case 'domingo':
print('Fin de Semana');
break;
default:
print('Día inválido!');
}
int calificacion = 0;
switch (calificacion) {
case >= 0 && <= 5:
print('No Suficiente');
break;
case 6:
print('Suficiente');
break;
case 7:
print('Bueno');
break;
case 8:
print('Muy Bueno');
break;
case 9:
print('Sobresaliente');
break;
case 10:
print('Excelente');
break;
}
}
When to Use Switch vs If-Else
Use switch for discrete values
Switch statements are ideal when checking a single variable against multiple specific values.
Use if-else for ranges
If-else statements are better for complex conditions or range checks (though Dart 3.0+ supports patterns in switch).
Consider readability
Choose the option that makes your code most readable for the specific use case.
Important Notes
Every case in a switch statement must have a terminating statement (break, continue, return, or throw). Empty cases can fall through to the next case.
Supported Types
Switch statements in Dart work with:
- Integers
- Strings
- Enums
- Compile-time constants
- Objects (with proper equality implementation)
Default Case
The default case is optional but recommended. It handles any value that doesn’t match the specified cases:
switch (value) {
case 'expected':
print('Matched!');
break;
default:
print('No match found');
}