Sub topic 7
Reusing one state type for different data
This chapter assumes Dart classes, null safety, basic generics, and Flutter build methods. A generic type lets one class work with different data types while keeping type checking. You can use a single sealed state hierarchy for a list of products, a user profile, or any other loaded value.
Why make the state generic?
A loading state usually carries no data. A loaded state carries a value. A failed state carries an error. The value can differ between features, so the state type should not force every feature to use String or List<String>.
LoadState<T> uses T as a placeholder for the loaded data type.
| State | Data carried | Example |
|---|---|---|
| `Loading<T>` | None | `Loading<List<String>>()` |
| `Loaded<T>` | A value of type `T` | `Loaded<List<String>>([...])` |
| `Failed<T>` | An error | `Failed<User>(error)` |
The type parameter appears on the base class and on every variant. This means Loaded<int> can contain only an integer, while Loaded<List<String>> can contain only a list of strings.
Declare the generic state
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) {
return switch (state) {
Loading() => 'Loading...',
Loaded(data: final items) => '${items.length} items',
Failed(error: final error) => 'Error: $error',
};
}
void main() {
const LoadState<List<String>> loading = Loading();
const LoadState<List<String>> loaded = Loaded(['red', 'blue']);
const LoadState<List<String>> failed = Failed('Network unavailable');
print(describe(loading));
print(describe(loaded));
print(describe(failed));
}The variable type LoadState<List<String>> means that all three variants belong to the same state family. The loaded variant must contain a List<String>. The loading and failed variants still use List<String> as the family’s data type, even though they do not currently store that data.
Keep operations type-safe
A function can accept a generic state and return its loaded value when available:
T? loadedValue<T>(LoadState<T> state) {
return switch (state) {
Loaded(data: final value) => value,
Loading() || Failed() => null,
};
}
void main() {
const LoadState<int> state = Loaded(42);
final int? value = loadedValue(state);
print(value); // 42
}The compiler knows that value is an int?. If the state were LoadState<User>, the same function would return User? without copying the state classes or writing another helper.
Use the same hierarchy across features
A Flutter screen can map a generic state to widgets while choosing its own data type:
flowchart TD
A["LoadState<T>"] --> B["Loading<T>"]
A --> C["Loaded<T> with data"]
A --> D["Failed<T> with error"]
C --> E["T = List<String>"]
C --> F["T = User"]
C --> G["T = Settings"]The sealed hierarchy supplies the control flow, and T supplies the feature-specific value. This keeps state classes reusable without losing the compiler’s checks.
A common mistake is to write LoadState<Object?> everywhere. That weakens type safety because Object? accepts almost anything. Prefer the most specific type the feature needs, such as LoadState<List<Product>> or LoadState<User>.
Exercise
What is the type of state.data in Loaded<User>?
Show answer
It is User, because the type parameter T is replaced with User.