fluttersmith/tipsGitHub ↗
Learning path · 10 tips, in order

Riverpod 3 from zero

From what a provider is to async state, arguments and safe updates. Written for Riverpod 3.

  1. 006The parts of a Riverpod 3 providerA provider is a global variable, a type, a body that gets a Ref, and optional modifiers. Here is what each part does.riverpod
  2. 012Which Riverpod 3 provider should you use?Six providers cover almost every job in Riverpod 3. StateProvider, StateNotifierProvider and ChangeNotifierProvider now live in legacy.dart.riverpod
  3. 009ref.watch, ref.read or ref.listen?Watch to rebuild, read for a one-off value inside a callback, listen to run side effects like SnackBars.riverpod
  4. 003Getting a Future from a FutureProviderWatch provider.future to await one async provider inside another, and combine the results with plain async code.riverpod
  5. 014One widget for AsyncValue loading and error statesWrap AsyncValue in a small widget with a switch over AsyncData, AsyncError and AsyncLoading, and every async screen writes only its data UI.riverpod
  6. 018AsyncValue.guard instead of try/catch in notifiersAsyncValue.guard runs a Future and hands back AsyncData or AsyncError, so notifier methods need no try/catch.riverpod
  7. 022Pass arguments to a Notifier through its constructorIn Riverpod 3 a family notifier is a plain Notifier that takes its argument in the constructor, so every method can use it as a field.riverpod
  8. 025Initialise async dependencies in main() and use requireValueAwait a FutureProvider once before runApp, then read it synchronously everywhere with requireValue. No loading states for things that are always ready.riverpod
  9. 030Check ref.mounted after an await in a providerA provider can be disposed while a Future is pending. Check ref.mounted after the await before you touch state, or Riverpod 3 throws.riverpod
  10. 027Migrate a StateNotifier to a NotifierStateNotifier now lives in legacy.dart. Moving to Notifier takes a few mechanical steps, and one behaviour change is worth knowing about.riverpod