Sub topic 6
Mutable State with Notifier and NotifierProvider
This chapter assumes you know Dart classes, generics, and Flutter's build method. It also assumes you understand ProviderScope and basic providers. You will use Notifier when state can change through named operations such as increment, decrement, or reset.
Why use a Notifier?
A Notifier is a class that owns mutable application state. The state value is exposed separately from the methods that change it.
NotifierProvider connects the Notifier to Flutter. Widgets can watch the provider's value and rebuild when that value changes. User actions can read the notifier and call one of its methods.
This keeps state-changing logic out of the widget:
button tap → notifier method → new state → widgets rebuildDeclare a NotifierProvider
The following notifier stores an integer counter. The build method returns the initial state. The state property contains the current value.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
class Counter extends Notifier<int> {
@override
int build() => 0;
void increment() => state++;
void decrement() => state--;
void reset() => state = 0;
}
final counterProvider =
NotifierProvider<Counter, int>(Counter.new);
void main() {
runApp(const ProviderScope(child: CounterApp()));
}
class CounterApp extends ConsumerWidget {
const CounterApp({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final count = ref.watch(counterProvider);
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Counter')),
body: Center(
child: Text(
'$count',
style: Theme.of(context).textTheme.displayMedium,
),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
heroTag: 'reset',
onPressed: () =>
ref.read(counterProvider.notifier).reset(),
child: const Icon(Icons.refresh),
),
const SizedBox(width: 12),
FloatingActionButton(
heroTag: 'increment',
onPressed: () =>
ref.read(counterProvider.notifier).increment(),
child: const Icon(Icons.add),
),
],
),
),
);
}
}NotifierProvider<Counter, int> means that the notifier class is Counter and the value exposed to widgets is an int.
Watch values and read commands
Use ref.watch(counterProvider) when the widget needs the current value. Watching creates a subscription, so the widget rebuilds after state changes.
Use ref.read(counterProvider.notifier) when an event handler needs to call a method. Reading the notifier does not subscribe the widget to notifier changes.
| Expression | Purpose | Typical location |
|---|---|---|
| `ref.watch(counterProvider)` | Read the state and rebuild on changes | `build` |
| `ref.read(counterProvider.notifier)` | Call a state-changing method | Button callback |
A notifier can hold an object instead of an integer. In that case, assign a new object when changing state. Replacing the value makes the update visible and keeps the state transition clear.
Common mistakes
- Calling
ref.readinbuildwhen the UI must update. Useref.watchfor displayed state. - Calling a notifier method during
build. This can create repeated updates or rebuild loops. - Putting all application logic in button callbacks. Move reusable rules into the notifier.
- Forgetting
ProviderScopenear the application root. Providers need a container in which to store their state.
Exercise
Which expression should a button use to increment the counter, and which expression should the displayed number use?
Show answer
The button should use ref.read(counterProvider.notifier).increment(). The displayed number should use ref.watch(counterProvider).