Sub topic 6
Letting the compiler check every case
Assume you know Dart classes, null safety, generics, and basic Flutter build methods. You also need Dart 3 or later, because sealed classes and pattern matching depend on Dart 3 language features.
A state model is useful only when every state is handled. Dart can check this for you when the base type is a sealed class and the states are represented by subclasses. An exhaustive switch is a switch that covers every possible subtype.
Exhaustive switch expressions
Consider a loading feature with three valid states:
sealed class LoadState<T> {
const LoadState();
}
final class Loading<T> extends LoadState<T> {
const Loading();
}
final class Loaded<T> extends LoadState<T> {
const Loaded(this.data);
final T data;
}
final class Failed<T> extends LoadState<T> {
const Failed(this.error);
final Object error;
}
String describe(LoadState<List<String>> state) => switch (state) {
Loading() => 'Loading...',
Loaded(data: final items) when items.isEmpty => 'Nothing here yet',
Loaded(data: final items) => '${items.length} items',
Failed(error: final error) => 'Error: $error',
};
void main() {
print(describe(const Loading()));
print(describe(const Loaded(['Tea', 'Coffee'])));
print(describe(const Failed('Network unavailable')));
}The switch is an expression because it produces a value. Each arrow returns one String, so describe can return the complete switch directly.
The compiler knows that LoadState<List<String>> can be Loading, Loaded, or Failed. If you remove the Failed case, Dart reports a compile-time error. This is safer than remembering to update a collection of if statements.
How exhaustiveness works
A sealed class defines a closed family of subtypes. The compiler can find the direct subtypes because they must be declared in the same library as the sealed class. It then checks switches that match the sealed type.
The relationship looks like this:
When you add another direct subtype, every exhaustive switch over that sealed type needs a new case. This is helpful during maintenance. The compiler points to the places where the new state needs a decision.
Patterns and guarded cases
Loaded(data: final items) is an object pattern. It checks that the value is a Loaded object and binds its data field to items.
The when clause is a guard. It adds a condition to an otherwise matching pattern. In the example, an empty list gets a special message, while the following unguarded Loaded case handles every other loaded list.
Keep an unguarded case for each variant when you need exhaustiveness. A guarded case alone does not prove that all objects of that variant are handled, because its condition might be false.
The order of cases matters. Put a more specific pattern before a general pattern. If the general Loaded case came first, the empty-list case would never be reached, and Dart would report the later case as unreachable.
Updating a model safely
Suppose you add this state:
final class Refreshing<T> extends LoadState<T> {
const Refreshing();
}The compiler now requires every switch over LoadState to decide what Refreshing means. You might show the previous data, display a progress indicator, or treat it as a separate screen. The important decision cannot be silently forgotten.
Exercise
Why is an unguarded Loaded case needed after the empty-list case?
Show answer
The guarded case matches only loaded states whose list is empty. The unguarded case handles all other Loaded states and completes coverage for that subtype.