ENGINEERING RESIDENCYBy Mobterest Studio

Free preview

What is State? Understanding the Problem

12 min2 points
TermMeaning
StateAny data that can change and requires a UI update
Local stateBelongs to one widget only → use setState
Shared stateNeeded by nearby widgets → lift to common ancestor
Global stateNeeded app-wide → use Riverpod
Prop drillingPassing data through widgets that don't need it
Reactive programmingDeclare dependencies; framework handles updates
ProviderContainer that holds and exposes state
ProviderScopeRoot widget; must wrap the entire app
ConsumerWidgetRiverpod-aware replacement for StatelessWidget
WidgetRefGives access to providers inside a widget
ref.watch()Subscribe to a provider; widget rebuilds on change
ref.read()Read a provider once; no rebuild
AsyncValueWraps async data: loading / error / data
NotifierClass that holds state and its change methods
ImmutabilityNever mutate state → always replace with a new object

Riverpod 2.x vs Riverpod 3.x

AreaRiverpod 2.xRiverpod 3.x
Recommended state APINotifier / AsyncNotifier, alongside StateNotifier and StateProviderNotifier / AsyncNotifier only
Legacy providersStateProvider, StateNotifierProvider, ChangeNotifierProvider imported normallyMoved to package:flutter_riverpod/legacy.dart
Failed providersStay in the error stateRetry automatically with exponential backoff (configurable)
Off-screen widgetsKeep listening to providersListeners are paused until the widget is visible again
Update filteringInconsistent (identical for some providers, == for others)Always uses ==
Error handlingOriginal exception is rethrownFailures are rethrown as ProviderException
Notifier lifecycleSame notifier instance can persist across rebuildsA new notifier is created whenever the provider rebuilds
Ref typeTyped refs per provider (e.g. FutureProviderRef, TodoRef)A single Ref everywhere
AutoDispose classesSeparate classes (e.g. AutoDisposeNotifier)Merged into the base classes
Family classesSeparate classes (e.g. FamilyNotifier)Merged; arguments are passed through the notifier's constructor
Checking if a ref is aliveNo built-in checkref.mounted (like BuildContext.mounted)
AsyncValue.valueOrNull.value
Side-effect UI stateHandled manuallyMutations (experimental)
Caching to diskHandled manuallyOffline persistence (experimental)
Generics in codegenNot supportedGenerated providers can define type parameters
TestingCreate ProviderContainer manuallyNew utilities such as ProviderContainer.test()
Missing overridesFound at runtimeNew 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.