Sub topic 5
`ref.watch` versus `ref.read` in Riverpod
Assume you know Dart classes, generics, and async/await. You should also understand Flutter widgets, the build method, and how setState updates a widget.
Riverpod gives a widget a WidgetRef. The WidgetRef is the widget’s connection to providers. The two most common ways to use it are ref.watch and ref.read.
The working model
ref.watch means:
> Read this provider and rebuild this widget when its value changes.
ref.read means:
> Read this provider once, without subscribing the widget to future changes.
Use watch for values that appear in the UI. Use read for one-time actions, usually inside callbacks.
| Operation | Subscribes to changes? | Typical location |
|---|---|---|
| `ref.watch(provider)` | Yes | `build` method |
| `ref.read(provider)` | No | Button callbacks and commands |
| `ref.read(provider.notifier)` | No | Calling a notifier method |
A subscription is a relationship that tells Riverpod to notify the widget when the provider changes.
Watching state for rebuilds
This example stores a counter in a Notifier. The widget watches the counter value, so the displayed number changes after every increment.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
class Counter extends Notifier<int> {
@override
int build() => 0;
void increment() {
state++;
}
}
final counterProvider =
NotifierProvider<Counter, int>(Counter.new);
void main() {
runApp(
const ProviderScope(
child: MaterialApp(home: CounterPage()),
),
);
}
class CounterPage extends ConsumerWidget {
const CounterPage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final count = ref.watch(counterProvider);
return Scaffold(
appBar: AppBar(title: const Text('Counter')),
body: Center(child: Text('$count')),
floatingActionButton: FloatingActionButton(
onPressed: () {
ref.read(counterProvider.notifier).increment();
},
child: const Icon(Icons.add),
),
);
}
}The build method uses ref.watch(counterProvider) because the text must update when count changes.
The button uses ref.read(counterProvider.notifier) because pressing the button is an action. The callback does not need to rebuild the widget merely because it obtained the notifier. The notifier changes the state, and the existing watch subscription handles the rebuild.
Common mistakes
Calling ref.read for a value displayed by the UI prevents automatic updates:
final count = ref.read(counterProvider);This reads the current value, but the widget is not subscribed to later changes. The button may update the provider while the displayed text remains unchanged until another rebuild happens for an unrelated reason.
Calling ref.watch inside a button callback is also usually incorrect. The callback is not where the widget declares its reactive UI dependencies. Watch the value in build, then read the notifier in the callback.
A widget can watch more than one provider. Each watch call creates a separate dependency, and a change in any watched provider can schedule a rebuild.
Exercise
A screen shows the current username and has a “Refresh” button. Which operation should render the username, and which operation should start the refresh command?
Show answer
Use ref.watch(userProvider) to render the username. Use ref.read(userProvider.notifier).refresh() inside the button’s onPressed callback.