Free preview
What is State? Understanding the Problem
12 min2 points
| Term | Meaning |
|---|---|
| State | Any data that can change and requires a UI update |
| Local state | Belongs to one widget only → use setState |
| Shared state | Needed by nearby widgets → lift to common ancestor |
| Global state | Needed app-wide → use Riverpod |
| Prop drilling | Passing data through widgets that don't need it |
| Reactive programming | Declare dependencies; framework handles updates |
| Provider | Container that holds and exposes state |
ProviderScope | Root widget; must wrap the entire app |
ConsumerWidget | Riverpod-aware replacement for StatelessWidget |
WidgetRef | Gives access to providers inside a widget |
ref.watch() | Subscribe to a provider; widget rebuilds on change |
ref.read() | Read a provider once; no rebuild |
AsyncValue | Wraps async data: loading / error / data |
Notifier | Class that holds state and its change methods |
| Immutability | Never mutate state → always replace with a new object |
Riverpod 2.x vs Riverpod 3.x
| Area | Riverpod 2.x | Riverpod 3.x |
|---|---|---|
| Recommended state API | Notifier / AsyncNotifier, alongside StateNotifier and StateProvider | Notifier / AsyncNotifier only |
| Legacy providers | StateProvider, StateNotifierProvider, ChangeNotifierProvider imported normally | Moved to package:flutter_riverpod/legacy.dart |
| Failed providers | Stay in the error state | Retry automatically with exponential backoff (configurable) |
| Off-screen widgets | Keep listening to providers | Listeners are paused until the widget is visible again |
| Update filtering | Inconsistent (identical for some providers, == for others) | Always uses == |
| Error handling | Original exception is rethrown | Failures are rethrown as ProviderException |
| Notifier lifecycle | Same notifier instance can persist across rebuilds | A new notifier is created whenever the provider rebuilds |
| Ref type | Typed refs per provider (e.g. FutureProviderRef, TodoRef) | A single Ref everywhere |
| AutoDispose classes | Separate classes (e.g. AutoDisposeNotifier) | Merged into the base classes |
| Family classes | Separate classes (e.g. FamilyNotifier) | Merged; arguments are passed through the notifier's constructor |
| Checking if a ref is alive | No built-in check | ref.mounted (like BuildContext.mounted) |
| AsyncValue | .valueOrNull | .value |
| Side-effect UI state | Handled manually | Mutations (experimental) |
| Caching to disk | Handled manually | Offline persistence (experimental) |
| Generics in codegen | Not supported | Generated providers can define type parameters |
| Testing | Create ProviderContainer manually | New utilities such as ProviderContainer.test() |
| Missing overrides | Found at runtime | New lint rules detect them statically |
Migration watch-outs
- Automatic retry: Failing tests may retry instead of failing fast, so disable retry in tests.
- Pause/resume: Off-screen providers stop updating until they're visible.
==filtering: Rebuilds may change if you mutate objects or override==.- Recreated notifiers: Notifier fields don't survive a rebuild.
Sign in to track your progress and earn points.
Test it on a real problem
Put what you just learned to work on a case real mobile teams get stuck on.