Riverpod separates provider declarations from widget-tree lookup and supplies a container through ProviderScope. A Notifier expresses operations that update its state. Keep those operations focused on a feature’s domain.
For an additional feature walkthrough, continue with the FlutterFever Riverpod guide . Compare its provider lifetime with the feature you are building.
Configure a local project
Add flutter_riverpod and import package:flutter_riverpod/flutter_riverpod.dart. Wrap the app with ProviderScope. The fragment below shows a model and the consumer region, not a complete application.
Keep state transitions explicit
Watching a provider makes the consumer react to state changes. Reading the notifier invokes an operation without subscribing that callback to future changes.
Worked example
This is a focused code fragment. Place it in the class or file described above; it is not a complete standalone app.
class SessionCount extends Notifier<int> {
@override
int build() => 0;
void complete() => state = state + 1;
}
final sessionProvider = NotifierProvider<SessionCount, int>(SessionCount.new);
// Inside a ProviderScope descendant:
Consumer(builder: (context, ref, child) {
final sessions = ref.watch(sessionProvider);
return FilledButton(
onPressed: () => ref.read(sessionProvider.notifier).complete(),
child: Text('$sessions sessions'),
);
})
Read the result
The provider describes a state resource; the scope/container owns provider instances. For a request, model loading and errors rather than replacing them with an initial zero value. Provider lifetime and automatic disposal depend on how that provider is declared.
Follow declaration, instance and update
The provider declaration describes how the state resource is created. ProviderScope supplies the container that owns actual provider instances. A consumer watches the value to render it, while reading the notifier lets an event invoke a state-changing method.
Those are separate concepts: declaring a provider does not mean every widget should watch it, and invoking an action does not mean the current callback must become a reactive consumer. Keep each relationship intentional.
For asynchronous resources, define loading, data and failure behavior and check the lifetime rules of the exact provider declaration you use. Use async UI states as the visible contract, and dependency injection to understand why replaceable dependencies make tests easier.
Practice and verification
Add a reset operation and write a container-level test without pumping the full interface. Then make an asynchronous version and list which visible states the screen must render.