Dart Sealed Classes Make Missing Switch Branches a Compile Error
Dart 3 sealed classes make missing switch branches a compile-time error, turning runtime state bugs into static analysis failures.
27 Mar 2026, 12:58 UTC

You add a new ApiState subtype—say, Refreshing—to a Dart project, rebuild, and everything compiles. Weeks later, a screen shows stale data because one switch somewhere never handles Refreshing. The bug is not a typo; it is a missing case that the type system did not catch.
The useful takeaway: in Dart 3, sealed classes combined with switch expressions make that omission a static analysis error. The compiler knows every subclass because they must live in the same library, so it can refuse to compile an incomplete switch.
Why ordinary hierarchies miss cases
A regular open class lets anyone subclass it in any library. The compiler cannot know the full list of subtypes, so a switch over the base type can compile even when a new subclass appears later. You end up writing defensive default branches or runtime is checks with casts, both of which hide missing logic until a user hits it.
Sealed classes and exhaustive switching
A sealed class cannot be instantiated directly, and every permitted subtype must be declared in the same library file or its part files. Because the complete hierarchy is visible to the analyzer, a switch expression over a sealed type is checked for exhaustiveness. Leave out a subclass, and Dart reports a static error instead of letting the code run.
Pattern matching lets a branch destructure fields inline, so you avoid manual is tests and casts.
Worked example: API response states
This example models three states for a network call. All classes live in one file, api_state.dart.
sealed class ApiState {}
class Loading extends ApiState {}
class Success extends ApiState {
final Object data;
Success(this.data);
}
class Failure extends ApiState {
final Object error;
final StackTrace? stackTrace;
Failure(this.error, [this.stackTrace]);
}
String describe(ApiState state) => switch (state) {
Loading() => 'Waiting for response...',
Success(data: var data) => 'Received: $data',
Failure(error: var e) => 'Failed: $e',
};
Run these commands from the project root:
dart --version
dart analyze lib/api_state.dart
Expected check: the first command prints the SDK version, which should be Dart 3 or later. The analyzer should report no issues for the complete switch above.
To see exhaustiveness in action, temporarily delete the Failure branch and run dart analyze again. The analyzer should flag the missing branch. Restore the branch and the error clears. Do not assume a specific error message; wording varies by SDK version.
The trade-off: deliberate coupling
Exhaustiveness is a two-way contract. Adding a new sealed subclass forces you to update every switch expression and statement that handles the type. That coupling is intentional: it surfaces incomplete logic at compile time instead of in production. If your hierarchy changes weekly, the churn can be significant; if it models a stable domain such as API states or UI screens, the safety usually outweighs the edits.
How to verify it works
- Run
dart --versionin your terminal. Confirm Dart 3.x.x. No special permissions are needed. - Set the language version in
pubspec.yamlto at least3.0underenvironment.sdk. - Create a sealed class with two subclasses in one library.
- Write a switch expression that handles only one branch.
- Run
dart analyzeand confirm a missing-branch error appears. - Add the missing branch and confirm the error clears.
Risk: none of these commands change project state. The only state change is editing source files, which you can revert with your version-control system if needed.
Closing
Sealed classes do not make every bug disappear, but they turn a common class of runtime omissions into a fixable static error. Start with a small domain type—an API state, a form status, or a navigation route—and let the analyzer enforce that every case is handled.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.