Phase 6 · Architecture, Testing & PerformanceModule 39~58 min read

Advanced State Management Patterns

Compare Provider, Riverpod, BLoC, reducers, and state machines by concrete pressures while keeping domain architecture independent.

What you'll learn

Compare Provider, Riverpod, BLoC, reducers, and state machines by concrete pressures while keeping domain architecture independent. The lesson turns the APIs into a repeatable engineering workflow instead of a collection of isolated snippets.

By the end of this lesson, you'll be able to:

  • Apply Provider tradeoffs in a production-shaped Flutter feature
  • Apply Riverpod concepts in a production-shaped Flutter feature
  • Apply BLoC in a production-shaped Flutter feature
  • Apply Reducers in a production-shaped Flutter feature

Core mental model

Connect each API to the decision it supports. Flutter code stays maintainable when state, ownership, lifecycle, and platform boundaries are explicit.

ConceptWhat it meansDecision rule
ReducerA pure function maps current state and an event to next stateUse when transitions must be explicit and exhaustively tested
BLoCEvents are transformed into states through a dedicated componentUse when event workflows and team conventions justify the structure
Provider scopeA dependency or state owner is attached to a subtreePlace it at the narrowest lifecycle shared by all consumers

Professional workflow

Work in small vertical slices and keep behavior observable from the first iteration.

  1. Define the explicit feature state machine boundary: user goal, inputs, visible states, ownership, and expected failures.
  2. Build the smallest working vertical slice with typed data and explicit dependencies.
  3. Represent loading, empty, success, and failure behavior where the feature can encounter them.
  4. Verify logic away from the UI, then exercise the rendered behavior at its public boundary.
  5. Inspect lifecycle, accessibility, performance, security, and platform behavior before widening the feature.
  6. Refactor only after behavior is protected by repeatable evidence.

Protect the frame

Keep build methods predictable, move side effects to explicit owners, and measure before introducing caches, isolates, or architectural layers.

Guided Flutter lab

Build a focused explicit feature state machine slice

This compact example keeps the important ownership and data-flow decisions visible so the behavior is easy to extend and test.

lib/progress_reducer.dart
sealed class ProgressEvent { const ProgressEvent(); }
class LessonCompleted extends ProgressEvent { const LessonCompleted(this.id); final String id; }
class ProgressReset extends ProgressEvent { const ProgressReset(); }

Set<String> reduceProgress(Set<String> state, ProgressEvent event) => switch (event) {
  LessonCompleted(id: final id) => {...state, id},
  ProgressReset() => <String>{},
};

void main() {
  assert(reduceProgress({}, const LessonCompleted('dart-1')).contains('dart-1'));
}

Production practice

Contract

Define the explicit feature state machine inputs, outputs, owner, lifecycle, visible states, and platform assumptions before selecting APIs or packages.

Verification

Protect pure rules with unit tests and the rendered public contract with widget or integration evidence; include one unavailable or failure case.

Operations

Keep dependencies replaceable, log actionable context without user secrets, and measure user-visible behavior before optimizing.

Common failure mode

Choosing a package before defining state ownership, events, derived values, and side effects lets framework vocabulary replace application design.

Independent workshop

Extend the guided lab into a review-ready explicit feature state machine feature that fits the running course portfolio app.

Your finished workshop must include:

  • Provider tradeoffs
  • Riverpod concepts
  • BLoC
  • Reducers
  • State machines
  • Automated verification and a short design note

Definition of done

Demonstrate the happy path, an empty or unavailable state, and at least one failure path. Add an automated check and a short note explaining one design decision.

Recap & quick check

Key takeaways

  • Reducer: Use when transitions must be explicit and exhaustively tested
  • BLoC: Use when event workflows and team conventions justify the structure
  • Provider scope: Place it at the narrowest lifecycle shared by all consumers

Quick check

1. Which rule best applies to Reducer?

2. Which rule best applies to BLoC?

3. Which rule best applies to Provider scope?

Next: Complete Flutter Testing Strategy