sealed Classes and Exhaustive switch
Close a hierarchy the compiler can check.
sealed Classes and Exhaustive switch is a free Dart Academy lesson on CoddyKit — lesson 2 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.
A Closed Family
A sealed class defines a fixed set of subtypes. No code outside its file can add a new one, so the family is fully known.
sealed class Shape {}Declaring Subtypes
Subclasses of a sealed type live in the same file. Together they form every possible variant the compiler must consider.
class Circle extends Shape {}
class Square extends Shape {}Cannot Instantiate Directly
A sealed class is implicitly abstract, so you never create one directly. You only ever build one of its known subtypes.
var s = Shape(); // error
var s = Circle(); // okWhy Sealed Helps
Because the subtypes are exhaustive, Dart knows the complete list. That lets switch verify you handled every single case.
Switch Over Subtypes
A switch on a sealed value can match each subtype by pattern. Each branch sees the precise type it matched.
switch (shape) {
case Circle(): print('round');
case Square(): print('boxy');
}Exhaustiveness Checked
If you forget a subtype, the compiler reports an error. You can never silently drop a case from a sealed switch.
No default Needed
Since every case is covered, you do not need a default branch. Adding one would only hide future missing cases.
switch as an Expression
Use switch as an expression to compute a value directly. Exhaustiveness still applies, so every branch returns something.
final name = switch (shape) {
Circle() => 'circle',
Square() => 'square',
};Adding a New Variant
Add a new subtype later and every switch that misses it lights up red. The compiler guides you to update each one.
class Triangle extends Shape {}
// existing switches now errorsealed vs abstract
An abstract class allows unknown outside subtypes, so switches can not be exhaustive. Sealed closes the set for full checking.
Great for State
Sealed types shine for modeling app states like Loading, Success, and Error. Each UI branch is then guaranteed to exist. ✅
sealed class Result {}
class Loading extends Result {}
class Done extends Result {}Quick Check
Quick check on sealed exhaustiveness.
Recap
You saw that sealed classes close a type family and make every switch exhaustive, so missing cases become compile errors. 🎯
Frequently asked questions
Is the “sealed Classes and Exhaustive switch” lesson free?
Yes — the full text of “sealed Classes and Exhaustive switch” 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 “sealed Classes and Exhaustive switch”?
Close a hierarchy the compiler can check. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “sealed Classes and Exhaustive switch” 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
- Final Fields and Deep Immutability
- sealed Classes and Exhaustive switch
- base, interface, and final Modifiers
- copyWith and Value Equality