Skip to contentEverything · newest firstAll 30 tips
- 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.State management
- 029Style an ElevatedButton with styleFrom and themesUse ElevatedButton.styleFrom for one button, ElevatedButtonThemeData for all of them, and WidgetStateProperty when a style depends on state.Widgets & layout
- 028Parse JSON safely with map patternsA switch on a map pattern checks keys and value types in one go, so bad JSON reaches your fallback case instead of a TypeError.Dart language
- 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.State management
- 026Show real progress with a value on progress indicatorsGive CircularProgressIndicator or LinearProgressIndicator a value from 0.0 to 1.0 and they show how far along you are instead of spinning.Widgets & layout
- 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.State management
- 024firstOrNull instead of try/catch on StateErrorfirst, firstWhere and single throw when nothing matches. The OrNull versions return null, and the type system makes you handle it.Dart language
- 023Make text selectable across widgets with SelectionAreaWrap a subtree in SelectionArea and one selection runs across all its Text widgets. SelectionContainer.disabled carves out the parts to skip.Widgets & layout
- 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.State management
- 021Skip nulls in collection literals with ...? and ?valueThe null-aware spread adds nothing for a null list, and a null-aware element leaves out a null entry, so you can drop the if (x != null).Dart language
- 020React to the app going to the background with AppLifecycleListenerAppLifecycleListener gives you one named callback per lifecycle transition. No observer mixin needed, but you must dispose it.Widgets & layout
- 019Extension types vs extension methodsAn extension method adds to a type. An extension type wraps it in a new static type, so a UserId can't be passed where an OrderId belongs.Dart language
- 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.State management
- 017Replace Container with the widgets it wrapsContainer is a bundle of Padding, ColoredBox, DecoratedBox, Align and SizedBox. Use those directly and the whole subtree can be const.Widgets & layout
- 016Await several futures with a record's .waitFuture.wait gives you a List of Object. A record of futures with .wait keeps each type and tells you which one failed.Dart language
- 015Give buttons the same width with IntrinsicWidthWrap a Column in IntrinsicWidth and stretch its children, and every button matches the widest one, whatever the text size.Widgets & layout
- 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.State management
- 013Destructure lists with list patternsList patterns pull values out of a list by position, and a rest element skips or collects whatever sits in the middle.Dart language
- 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.State management
- 011Return SizedBox.shrink() when there is nothing to showA build method must return a widget. For "nothing", return const SizedBox.shrink(), not an empty Container.Widgets & layout
- 010Return multiple values with a recordA record returns two or more values without a throwaway class, and destructuring unpacks them in one line at the call site.Dart language
- 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.State management
- 008Switch on a record to match several values at oncePut two or three values in a record and switch on it. Each case is a row in a table, instead of a tangle of if/else.Dart language
- 007Use MediaQuery.sizeOf instead of MediaQuery.ofMediaQuery.of rebuilds your widget when anything changes. MediaQuery.sizeOf and friends rebuild only when the value you read changes.Widgets & layout
- 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.State management
- 005A Result type with a sealed classReturn success or failure instead of throwing, and let an exhaustive switch make sure every caller handles both.Dart language
- 004Check context.mounted after every awaitA BuildContext can go stale while you await. Check context.mounted before you use it again, or grab what you need first.Widgets & layout
- 003Getting a Future from a FutureProviderWatch provider.future to await one async provider inside another, and combine the results with plain async code.State management
- 002Column(spacing:) replaces a SizedBox between every childRow, Column and Flex take a spacing value, so you no longer need a SizedBox or the gap package between children.Widgets & layout
- 001Measure execution time with StopwatchDateTime.now() reads the wall clock, which can jump. Stopwatch can't, so use it to time code.Dart language
Keyboard shortcuts
- /
- Search
- r
- Random tip
- ← →
- Previous / next tip
- c
- Copy the first snippet
- p
- Debug paint on / off
- t
- Theme: system, light, dark
- ?
- This list