Sub topic 2
Setting up `riverpod_generator` and `build_runner`
This chapter assumes that you know Dart fundamentals, including async and Future, and that you have used ProviderScope, ConsumerWidget, and ref.watch in Riverpod. You should also be able to run Dart commands from a terminal.
Riverpod code generation lets you describe a provider with an annotation instead of writing the provider declaration by hand. A generator reads the annotated Dart file and creates a companion .g.dart file. The generated file contains the provider objects that your widgets and other providers use.
Install the packages
A Flutter project needs three packages:
riverpod_annotationprovides the@riverpodannotation.riverpod_generatorreads annotated Dart code and creates provider code.build_runnerruns the generator.
Add the runtime package as a regular dependency. Add the generator and build_runner as development dependencies:
flutter pub add flutter_riverpod riverpod_annotation
flutter pub add --dev riverpod_generator build_runnerflutter_riverpod is still needed when Flutter widgets read providers. The annotation package is used by the source file. The generator packages are normally not included in the application bundle.
Generate the provider files
Run this command from the project directory:
dart run build_runner buildThe command scans the project and writes generated files. Run it after adding or changing an annotation.
During development, watch mode reruns the generator whenever you save a file:
dart run build_runner watchStop watch mode with Ctrl+C. Generated files usually have a .g.dart suffix, such as greeting.g.dart. Commit these files only if your project workflow requires generated output to be committed. Follow the convention already used by your repository.
Add a generated provider
The source file must include a part directive. Its value must match the source file's name.
For example, save this code in greeting.dart:
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'greeting.g.dart';
@riverpod
String greeting(Ref ref) => 'Hello';
@riverpod
Future<int> answer(Ref ref) async => 42;Then run:
dart run build_runner buildThe annotation tells Riverpod to generate providers from the functions. The function name determines the provider name:
greetingproducesgreetingProvider.answerproducesanswerProvider.
The return type determines the provider's behavior. A synchronous String return value produces a regular Provider<String>. A Future<int> return value produces a FutureProvider<int>.
The Ref parameter gives the provider access to Riverpod's dependency and lifecycle APIs. You can use it to watch another provider, read a value once, or register cleanup work. The generated provider is available only after code generation succeeds.
Check the generated file
After generation, greeting.g.dart contains declarations that your application can import indirectly through greeting.dart. In most application code, import the source file rather than importing the generated file directly:
import 'greeting.dart';
final message = ref.watch(greetingProvider);
final result = ref.watch(answerProvider);A generated file is an implementation detail of the annotated source file. The part directive connects the two files so that Dart treats them as one library.
Troubleshoot generation errors
Most setup errors come from a small set of mismatches:
- The
partfilename does not match the source filename. - The generated file is missing because
build_runnerhas not been run. - A package was added but dependencies were not fetched successfully.
- The annotation or generator packages use incompatible versions.
- An old generated file remains after a source change.
If generated output appears stale, run a clean build:
dart run build_runner clean
dart run build_runner buildUse clean when normal generation does not remove outdated output. Do not edit .g.dart files by hand. The next generation run will replace those edits.
Exercise
Which command continuously regenerates .g.dart files while you edit annotated Dart files?
Show answer
dart run build_runner watch