0Pricing
Dart Academy · Lesson

switch Patterns and Guards

Match shapes and add when conditions.

switch Meets Patterns

Modern Dart lets switch match patterns, not just constants. Each case can test the shape of a value and unpack it at once. 🔀

Matching a Record Shape

A case can be a record pattern. If the value matches that shape, the case runs and its fields bind to variables.

switch (point) {
  case (0, 0): print('origin');
}

All lessons in this course

  1. Records: Lightweight Tuples
  2. Positional and Named Record Fields
  3. Destructuring With Patterns
  4. switch Patterns and Guards
← Back to Dart Academy