Dart Switch Expressions
last modified June 4, 2025
In Dart, switch expressions offer a concise and powerful way to perform pattern matching. Introduced in Dart 3.0, they enhance readability and provide a more expressive alternative to traditional switch statements.
Unlike conventional switch statements, switch expressions evaluate to a value, allowing for more flexible control flow. They support exhaustive checking, ensuring that all possible cases are handled, and they integrate advanced pattern matching, enabling sophisticated conditional logic with minimal code.
Basic Switch Expression
The simplest form of switch expression matches constant values. It returns a value based on the matched case.
void main() { var day = 'Monday'; var message = switch (day) { 'Monday' => 'Start of work week', 'Friday' => 'Almost weekend', 'Saturday' || 'Sunday' => 'Weekend!', _ => 'Midweek day', }; print(message); }
This example shows a basic switch expression matching string values. The underscore (_) is the default case. The expression returns a string assigned to message.
$ dart main.dart Start of work week
Matching Enum Values
Switch expressions work particularly well with enums. Dart ensures exhaustive checking when switching on enums.
enum Status { pending, approved, rejected } void main() { var currentStatus = Status.approved; var response = switch (currentStatus) { Status.pending => 'Please wait', Status.approved => 'Request approved', Status.rejected => 'Request denied', }; print(response); }
The switch expression covers all possible enum values. Dart will warn if any enum value is missing. This prevents runtime errors from unhandled cases.
$ dart main.dart Request approved
Pattern Matching with Records
Switch expressions can destructure and match against Dart records. This allows complex pattern matching in a concise syntax.
void main() { var point = (2, 3); var quadrant = switch (point) { (0, 0) => 'Origin', (var x, var y) when x > 0 && y > 0 => 'Quadrant I', (var x, var y) when x < 0 && y > 0 => 'Quadrant II', (var x, var y) when x < 0 && y < 0 => 'Quadrant III', (var x, var y) when x > 0 && y < 0 => 'Quadrant IV', (_, _) => 'On axis', }; print(quadrant); }
This example matches a record representing a point. It uses when guards for conditional matching. The variables x and y are extracted from the record.
$ dart main.dart Quadrant I
Matching Object Types
Switch expressions can match and cast object types. This is useful for handling different types in a type-safe way.
abstract class Shape {} class Circle implements Shape { final double radius; Circle(this.radius); } class Square implements Shape { final double side; Square(this.side); } void main() { Shape shape = Circle(5.0); var area = switch (shape) { Circle(radius: var r) => 3.14 * r * r, Square(side: var s) => s * s, _ => 0.0, }; print('Area: $area'); }
The switch expression matches the concrete type of Shape. It extracts properties using object patterns. This provides type-safe property access without explicit casting.
$ dart main.dart Area: 78.5
Advanced Pattern Matching
Switch expressions support complex patterns including nested patterns and logical operators. This enables sophisticated matching scenarios.
void main() { var data = [ ['admin', '12345'], ['user', 'password'], ['guest', null] ]; for (var creds in data) { var access = switch (creds) { ['admin', _] => 'Full access', ['user', var pwd?] when pwd.length >= 8 => 'User access', ['user', _] => 'Weak password', [_, null] => 'No password', _ => 'Unknown user', }; print('${creds[0]}: $access'); } }
This example shows nested list patterns with conditional logic. The patterns match both the structure and content of the lists. Guards provide additional conditions.
$ dart main.dart admin: Full access user: Weak password guest: No password
Best Practices
- Exhaustiveness: Ensure all possible cases are covered.
- Readability: Keep patterns simple and clear.
- Type Safety: Use pattern matching for type-safe casting.
- Guards: Use when clauses for complex conditions.
Source
Dart Switch Expressions Documentation
This tutorial covered Dart's switch expressions with practical examples demonstrating their pattern matching capabilities and concise syntax.
Author
List all Dart tutorials.