Sub topic 12
Same-library rule and wildcard pitfalls
Assume you know Dart classes, null safety, generics, and basic Flutter widgets. This chapter focuses on two details that often surprise developers after they start using sealed classes: where subtypes may be declared and how wildcard patterns can hide missing behavior.
The same-library rule
A sealed class defines a closed family of direct subtypes. Dart requires the sealed class and all of its direct subclasses to be in the same library. In a simple project, a library is usually one Dart file. A library can also contain part files.
// load_state.dart
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;
}This declaration is valid because Loading and Loaded are in the same library as LoadState.
A class in another library cannot directly extend, implement, or mix in the sealed class:
// feature_state.dart
import 'load_state.dart';
// Compile-time error: LoadState is sealed in another library.
final class Failed<T> extends LoadState<T> {
const Failed(this.error);
final Object error;
}This restriction lets the analyzer see the complete set of direct variants when it checks a switch. Without the restriction, a package could add an unknown subtype in another file, making exhaustive checking unreliable.
A subtype declared in the same library may be open for further extension:
// load_state.dart
sealed class LoadState<T> {
const LoadState();
}
class DataState<T> extends LoadState<T> {
const DataState(this.data);
final T data;
}
// another_library.dart
import 'load_state.dart';
final class SpecialData extends DataState<String> {
const SpecialData(super.data);
}The direct subtype DataState is known to the sealed library. SpecialData is an indirect subtype, so code outside the library can extend DataState because DataState is not itself sealed.
Wildcards can hide future cases
The _ pattern matches anything and binds nothing. It is useful when a value is irrelevant:
String label(LoadState<List<String>> state) => switch (state) {
Loading() => 'Loading',
Loaded(data: final items) => '${items.length} items',
};If the hierarchy also contains Failed, this switch is incomplete. Adding a wildcard makes it compile:
String label(LoadState<List<String>> state) => switch (state) {
Loading() => 'Loading',
Loaded(data: final items) => '${items.length} items',
_ => 'Unknown',
};The wildcard is exhaustive, but it also absorbs every current and future variant. If you later add Refreshing, the analyzer will not force you to decide whether Refreshing should display “Loading”, “Unknown”, or a new message.
Use a wildcard only when all unmatched variants genuinely have the same behavior. Prefer named cases when each state has different meaning.
A wildcard inside an object pattern is narrower:
bool isLoaded(LoadState<List<String>> state) => switch (state) {
Loaded(data: _) => true,
_ => false,
};Here, data: _ ignores the loaded data. It does not ignore other state variants.
Guards and fallback cases
A when guard adds a condition to a pattern. A guarded case may match only part of a variant, so provide another case for the remaining values:
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',
_ => 'Could not load items',
};The final wildcard handles Failed. It is acceptable here because the example intentionally gives every failure the same text. If failures need logging, retry actions, or different UI, match Failed explicitly instead.
Exercise
Why is case _ => 'Unknown' risky in a switch over a sealed state?
Show answer
It makes the switch exhaustive immediately and silently handles any future state, so the analyzer cannot force a deliberate UI decision for a newly added variant.