Sub topic 1
Where Hand-Written Providers Get Repetitive
Riverpod providers are Dart objects that expose values, services, or state to widgets. In a small app, writing them by hand is simple. As an app grows, provider declarations often repeat the same structure: a provider type, a creation function, a Ref parameter, and sometimes a family or auto-dispose configuration.
This chapter assumes you know Dart functions, Future, Flutter widgets, ProviderScope, ConsumerWidget, and basic ref.watch usage.
Prerequisites
You should be able to:
- Read a Dart function that returns a value or a
Future. - Explain that
ref.watchsubscribes to another provider. - Place a
ProviderScopeabove widgets that use Riverpod. - Run Dart commands from a terminal.
The Repetition in Hand-Written Providers
A hand-written provider describes how Riverpod should create a value. For example, this provider exposes a constant greeting:
final greetingProvider = Provider<String>((ref) {
return 'Hello';
});A provider that loads data asynchronously uses a different provider class:
final answerProvider = FutureProvider<int>((ref) async {
return 42;
});The value-producing code is short. Most of the declaration tells Riverpod which kind of provider to create. The type appears both in the provider class and in the function's return type.
As the application grows, this pattern becomes repetitive:
final apiClientProvider = Provider<ApiClient>((ref) {
return ApiClient();
});
final settingsProvider = FutureProvider<Settings>((ref) async {
final api = ref.watch(apiClientProvider);
return api.loadSettings();
});Every provider needs a top-level variable with a unique name. The provider class must match the behavior: Provider, FutureProvider, StreamProvider, or a notifier provider. A family needs additional configuration. Auto-dispose needs another provider type or modifier.
The declarations are valid, but the structure can hide the important part: the function that creates the value.
Code Generation as a Declaration Shortcut
Riverpod code generation lets you annotate an ordinary Dart function or class. A generator reads the annotation and writes the repetitive provider declaration into a generated .g.dart file.
The handwritten source can focus on the provider's behavior:
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;The @riverpod annotation marks a function for generation. The part directive connects this file to the generated Dart file. The generator creates providers named after the functions, with a Provider suffix:
greetingproducesgreetingProvider.answerproducesanswerProvider.
The return type determines the generated provider type. A String return value produces a regular provider. A Future<int> return value produces a future provider. A Stream<T> return value produces a stream provider.
The generated file is source code that your application compiles. Do not edit it manually. Change the annotated source and run the generator again.
Why This Helps
Code generation reduces several sources of drift:
- The function name and provider name stay connected.
- The return type determines the provider kind.
- Generated types provide compile-time checks.
- Families can be expressed with normal function parameters.
- Notifier classes can use normal methods while generated code handles the provider wrapper.
This does not remove the need to understand providers. You still choose what a provider returns, which dependencies it watches, and how long its state should live. Code generation removes declaration bookkeeping so those decisions are easier to see.
Generated Names Are Part of the API
When a function named settings is generated, application code normally reads settingsProvider:
class SettingsText extends ConsumerWidget {
const SettingsText({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final settings = ref.watch(settingsProvider);
return Text(settings.themeName);
}
}The generated provider is available only after generation has run successfully. If the editor reports that settingsProvider does not exist, first check the part filename and regenerate the files.
Hand-Written and Generated Forms
Both forms use the same Riverpod runtime. A generated provider is not a separate state-management system. The main difference is where the declaration lives.
| Concern | Hand-written provider | Generated provider |
|---|---|---|
| Provider declaration | Written directly | Produced from an annotation |
| Main logic | Creation callback | Annotated function or class |
| Provider name | Chosen manually | Derived from the source name |
| Family parameters | Extra provider configuration | Function parameters |
| Generated file | Not required | Required |
The generated approach is most useful when an app has many providers, families, or notifier classes. For one tiny constant, a hand-written provider may remain perfectly readable.
Exercise
If an annotated function returns Future<List<String>>, which provider name and provider behavior should you expect?
Show answer
Expect a provider named after the function with a Provider suffix. Because the return type is Future<List<String>>, it behaves as a FutureProvider and exposes asynchronous loading states.