Lesson 5 of 9 · 28m
State Management: Choose by Ownership, Lifetime, and Failure Mode
Compare setState, Provider, Riverpod, Bloc, and immutable stores by the questions that matter in production rather than fashion.
State management arguments generate heat because teams compare package names instead of state lifetimes. There is no universally correct answer. There are incorrect assignments: ephemeral text input stored globally, server data hand-copied into widget state, payment flow state split across three controllers, and a cart that vanishes because someone scoped it to a route by accident.
Start with four ownership questions
Before choosing a mechanism, ask:
Who owns the truth?
How long does it live?
Who can mutate it?
What must happen when mutation fails?A checkbox animation is widget-local. A form draft may belong to one screen. Authenticated session state belongs above routing. Server collections often belong in a cache with explicit freshness. Device connectivity is external state, not something a widget should poll privately.
setState is not a moral failure
setState is correct for genuinely local, synchronous UI state: expansion, selected tab, hover, focus, small animation triggers. It fails when distant widgets need the same value, mutation order becomes complex, or business logic starts living inside build methods. The boundary is not size; it is ownership.
Riverpod and Provider model dependency and lifetime
Provider-family solutions shine when dependencies must be injected, overridden in tests, disposed correctly, and scoped outside the widget tree:
final authControllerProvider =
StateNotifierProvider<AuthController, AuthState>((ref) {
return AuthController(ref.watch(authRepositoryProvider));
});
final currentUserProvider = Provider<User?>((ref) {
return ref.watch(authControllerProvider).user;
});The important win is not syntax. It is explicit dependency direction. Tests can replace the repository without touching platform channels or a real backend. The danger is provider sprawl: hundreds of tiny globally visible pieces with no documented ownership. Naming and module boundaries still matter.
Bloc makes transitions explicit
Bloc/Cubit is strong where workflows have commands, transitions, and auditability: checkout, onboarding, synchronization, device pairing. A reducer-style state machine can make illegal states impossible:
sealed class CheckoutState {}
final class CheckoutEditing extends CheckoutState {}
final class CheckoutSubmitting extends CheckoutState {}
final class CheckoutFailed extends CheckoutState {
CheckoutFailed(this.error);
final AppError error;
}
final class CheckoutComplete extends CheckoutState {}This is valuable when the business process is complex. It is ceremony when the screen merely displays a server list.
Server state is not client state
One of the most expensive mistakes is treating server data as locally owned truth. If the server owns it, model freshness, loading, error, mutation, invalidation, and conflict explicitly. Packages such as dio, http, or GraphQL clients solve transport, not cache policy. You may not need a full offline sync engine, but you do need to decide which screens can show stale data and how users trigger reconciliation.
The decision table
local visual state StatefulWidget/setState
simple dependency injection Provider
testable app-wide dependencies Riverpod
complex workflows Bloc/reducer
asynchronous event streams Stream + explicit controller
server cache and sync dedicated repository/cache layerMixing mechanisms is acceptable when each owns a distinct layer. Mixing them for the same object is how impossible bugs are born.
Field notes
- Never store derived state without a invalidation rule; calculate it or cache it with the source generation.
- Mutable singletons make tests fast to write and dangerous to trust.
- If a provider or bloc is disposed while work continues, define whether the operation should cancel or complete detached.
- Global state is not bad; unowned global state is bad.
- When debugging duplicate network calls, inspect widget lifecycle and hot restart before assuming the state package is broken.
Was this lesson helpful?