Sub topic 11
Modelling transitions with (state, event) records
Assume you know Dart classes, generics, null safety, and basic Flutter widgets. You also need Dart 3 or later, because sealed classes, patterns, and records are Dart 3 features.
A screen state describes what is true now. An event describes something that happened or was requested. A state transition is the rule that calculates the next state from both values.
For example, a Fetch event should put any screen into Loading. A Succeeded event should replace Loading with Loaded. These rules belong in a small reducer, which is a pure function that returns a new state without changing the old state.
Match the state and event together
Dart records let you group values without creating another class. The expression (state, event) creates a two-value record. A record pattern in a switch can match both values at once.
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;
}
sealed class Event {
const Event();
}
final class Fetch extends Event {
const Fetch();
}
final class Succeeded extends Event {
const Succeeded(this.items);
final List<String> items;
}
final class Errored extends Event {
const Errored(this.error);
final Object error;
}
LoadState<List<String>> reduce(
LoadState<List<String>> state,
Event event,
) {
return switch ((state, event)) {
(_, Fetch()) => const Loading<List<String>>(),
(Loading(), Succeeded(items: final items)) => Loaded(items),
(Loading(), Errored(error: final error)) => Failed(error),
(Loaded() || Failed(), Succeeded() || Errored()) => state,
};
}
void main() {
var state = const Loading<List<String>>();
state = reduce(state, const Succeeded(['Ada', 'Grace']));
print(state is Loaded<List<String>>); // true
state = reduce(state, const Errored('Network error'));
print(state is Loaded<List<String>>); // true
}The first pattern, (_, Fetch()), uses _ for any current state. A fetch always starts loading.
The second pattern matches two things at once: the current state must be Loading, and the event must be Succeeded. The object pattern Succeeded(items: final items) extracts the event's items field.
The final case groups events that have no effect after a successful load or failure. Returning state keeps the current state unchanged.
Why exhaustiveness matters
Because LoadState and Event are sealed, the compiler knows their permitted subtypes in the relevant library. The switch must cover every combination. If you add a Cancelled event, the reducer will require a matching case before it compiles.
This check is safer than a default case such as _ => state. A broad default can silently ignore a new event. Use a wildcard only when that behavior is intentional and documented.
> callout note > Keep reducers pure. Start network requests, timers, and navigation outside the reducer. Pass their results into the reducer as events.
Test transitions as plain Dart
A reducer does not need Flutter or a widget test. Test each meaningful pair of inputs:
LoadingplusSucceededproducesLoaded.LoadingplusErroredproducesFailed.LoadedplusFetchproducesLoading.LoadedplusErroredkeepsLoaded.
This makes invalid transition rules easy to find before they affect the UI.
Exercise
Add a Refresh event that behaves like Fetch in the reducer. Which pattern should you change?
Show answer
Add final class Refresh extends Event { const Refresh(); }, then change the first case to (_, Fetch() || Refresh()) => const Loading<List<String>>(),.