0Pricing
Dart Academy · Lesson

switch Patterns and Guards

Match shapes and add when conditions.

switch Patterns and Guards is a free Dart Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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');
}

Binding While Matching

Patterns can capture values as they match. Name the slots and you get the fields ready to use inside the case body.

switch (p) {
  case (var x, var y): print(x + y);
}

The Wildcard Case

An underscore case matches anything. It works like a default, catching values no earlier case handled.

switch (n) {
  case 1: print('one');
  case _: print('other');
}

What Is a Guard?

A guard adds a condition to a case with the when keyword. The case fires only if the pattern matches and the test is true.

case (var x, var y) when x == y:
  print('diagonal');

Guards Refine a Match

Use a guard when the shape alone is not enough. It lets you split one pattern into finer outcomes based on the values.

Order Decides the Winner

Cases are checked top to bottom. The first matching case wins, so put your most specific patterns before broader ones.

switch as an Expression

A switch expression returns a value. Cases use the arrow, and the whole switch evaluates to one result.

var label = switch (n) {
  1 => 'one',
  _ => 'many',
};

No Break Needed

Pattern cases do not fall through, so you never write break. Each case is self-contained and tidy.

Logical-Or Patterns

Join patterns with || to match several shapes in one case. Handle related values together without repeating bodies.

case 1 || 2 || 3:
  print('small');

Exhaustive and Safe

For sealed types, the compiler checks your switch is exhaustive. Miss a case and Dart warns you before runtime ever does.

Quick Check

Which keyword adds an extra condition to a switch case pattern?

Recap: Patterns and Guards

You did it! 🎯 switch can match patterns, capture fields, refine matches with when guards, and return values as an expression.

Frequently asked questions

Is the “switch Patterns and Guards” lesson free?

Yes — the full text of “switch Patterns and Guards” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “switch Patterns and Guards”?

Match shapes and add when conditions. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “switch Patterns and Guards” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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