0Pricing
Dart Academy · Lesson

switch Over Enums Exhaustively

Handle every case the compiler checks.

switch Over Enums Exhaustively 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 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");
}

Exhaustiveness Means Every Case

Over an enum, Dart checks that you handle every member. Miss one and the analyzer warns you before you run.

Why Exhaustiveness Helps

This safety net is gold: add a new enum member later and the compiler points to every switch you must update.

The switch Expression

A switch expression returns a value instead of running statements. Use the fat arrow to map each case to a result.

var label = switch (status) {
  Status.pending => "Waiting",
};

Arrow Cases Are Concise

In a switch expression each case uses case-value => result. Commas separate them, making a compact lookup table.

Status.shipped => "On the way",

No Fall-Through in Dart

Unlike some languages, Dart cases do not fall through. Each case runs only its own code, so no stray break is needed.

The Wildcard Default

A default or _ case catches anything you did not list. But over enums, prefer handling each member for full safety.

default => "Unknown",

Default Disables the Check

Adding a catch-all default satisfies the switch, so Dart stops warning about missing members. Use it sparingly.

Sharing One Body

Let two members share a branch by listing them together. Both run the same code without repeating yourself.

case Status.shipped || Status.delivered: print("Sent");

Exhaustive Switching Is a Habit

Skipping the default on enums is a feature, not a chore. It turns the compiler into a checklist for future changes.

Quick Check

What does exhaustive switching over an enum give you?

Recap: Exhaustive switch

You saw switch handle each enum case, return values as an expression, and warn when a member is unhandled. Skip default for safety. 🎯

Frequently asked questions

Is the “switch Over Enums Exhaustively” lesson free?

Yes — the full text of “switch Over Enums Exhaustively” 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 Over Enums Exhaustively”?

Handle every case the compiler checks. 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 Over Enums Exhaustively” 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. Declaring and Using Plain Enums
  2. Enum Values, index, and name
  3. Enhanced Enums With Fields and Methods
  4. switch Over Enums Exhaustively
← Back to Dart Academy