Sub topic 5
Destructuring state with object patterns
This chapter assumes Dart 3 or later, plus knowledge of classes, constructors, final fields, null safety, generics, and basic Flutter widgets. It also assumes you have seen a sealed state hierarchy such as Loading, Loaded, and Failed.
Object patterns let a switch read the fields of a state object and bind them to local variables. This avoids writing type checks followed by casts or field access.
Reading fields from state variants
Suppose the states are defined like this:
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;
}The Loaded class has a getter named data, because final T data creates a getter. An object pattern can match that getter and bind its value:
Loaded(data: final items)Here, Loaded checks the runtime type. data: selects the getter. final items creates a local variable containing the getter's value.
The pattern does not depend on the constructor's parameter name. It uses the object's accessible getter name. This distinction matters when a constructor parameter and a field have different names.
A complete switch with destructuring
The following program describes every possible 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) => switch (state) {
Loading() => 'Loading...',
Loaded(data: final items) => '${items.length} items',
Failed(error: final error) => 'Error: $error',
};
void main() {
print(describe(const Loading<List<String>>()));
print(describe(const Loaded<List<String>>(['A', 'B'])));
print(describe(const Failed<List<String>>('Network unavailable')));
}Loading() matches a Loading object without extracting fields. Loaded(data: final items) extracts the list. Failed(error: final error) extracts the error object.
The value returned by each branch must have the same type. In this example, every branch returns a String, so the switch expression can be used directly as the function body.
| Pattern | What it checks | What it binds |
|---|---|---|
| `Loading()` | The object is a `Loading` state | Nothing |
| `Loaded(data: final items)` | The object is a `Loaded` state | `items` |
| `Failed(error: final error)` | The object is a `Failed` state | `error` |
Applying the pattern in Flutter
A widget can use the same expression to select a subtree:
return switch (state) {
Loading() => const Center(child: CircularProgressIndicator()),
Loaded(data: final items) => ListView(
children: [
for (final item in items) ListTile(title: Text(item)),
],
),
Failed(error: final error) => Center(
child: Text('Something went wrong: $error'),
),
};Each branch receives only the data that belongs to that state. A loading state cannot accidentally display stale items unless those items are explicitly added to its class.
Exercise
Exercise
In Loaded(data: final items), what does data: refer to, and what does final items create?
Show answer
data: refers to the Loaded object's getter named data. final items creates a local variable containing that getter's value.