Sub topic 1
Where hand-written providers get repetitive
This chapter shows why code generation helps with Riverpod providers and how to set it up. It also covers writing providers as annotated functions and reading other providers with ref.
Prerequisites
You should already know:
- Dart basics, including
async/awaitandFuture. - Flutter widgets such as
StatelessWidgetandConsumerWidget. - Basic Riverpod use:
ProviderScopeandref.watch. - Running
flutteranddartcommands in a terminal.
Where hand-written providers get repetitive
A hand-written provider is a variable that holds a Riverpod provider object. For a simple value, it looks like this:
import 'package:flutter_riverpod/flutter_riverpod.dart';
final greetingProvider = Provider<String>((ref) => 'Hello');Each new provider needs a variable, a type argument, and a matching constructor call. Families, notifiers, and auto-dispose settings add more boilerplate. Riverpod's generator reads an annotation such as @riverpod and writes that boilerplate for you.
Setting up riverpod_generator and build_runner
Add the runtime package, the annotation package, the generator, and build_runner:
flutter pub add flutter_riverpod riverpod_annotation
flutter pub add --dev riverpod_generator build_runner
# generate once
dart run build_runner build
# or regenerate on every save
dart run build_runner watchbuild_runner writes a .g.dart file next to each annotated file. Your source file must include a part directive that names it.
Each new provider needs a variable, a type argument, and a matching constructor call. Families, notifiers, and auto-dispose settings add more boilerplate. Riverpod's generator reads an annotation such as @riverpod and writes that boilerplate for you.
Setting up riverpod_generator and build_runner
Add the runtime package, the annotation package, the generator, and build_runner:
flutter pub add flutter_riverpod riverpod_annotation
flutter pub add --dev riverpod_generator build_runner
# generate once
dart run build_runner build
# or regenerate on every save
dart run build_runner watchbuild_runner writes a .g.dart file next to each annotated file. Your source file must include a part directive that names it.
Writing providers as annotated functions
Annotate a top-level function with @riverpod. The generator creates a provider named after the function, with a Provider suffix.
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'greeting.g.dart';
@riverpod
String greeting(Ref ref) => 'Hello';The generated provider is named greetingProvider. A function that returns Future<int> produces a FutureProvider-style provider with the same naming rule.
Reading other providers with ref
The ref parameter lets one provider read another. Use ref.watch inside a provider or widget build method, so the value updates when the dependency changes. Use ref.read only for a one-time read, such as inside a button handler.
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'answer.g.dart';
@riverpod
String greeting(Ref ref) => 'Hello';
@riverpod
int greetingLength(Ref ref) {
final text = ref.watch(greetingProvider);
return text.length;
}Exercise
In the greetingLength example, why does the code use ref.watch instead of ref.read?
Show answer
ref.watch subscribes to greetingProvider, so greetingLength recalculates when greeting changes. ref.read takes a single snapshot and does not update.