Sub topic 4
Reading Providers with ConsumerWidget and WidgetRef
This chapter assumes Dart classes, generics, and callbacks, plus Flutter widgets and the build method. It also assumes you know that setState causes a widget to rebuild. The examples use Riverpod 2-style APIs.
A provider is a declaration outside the widget tree. It describes how to create or obtain a value. A widget does not need to receive that value through a constructor. Instead, a Riverpod-aware widget reads it through WidgetRef.
ConsumerWidget gives a widget access to providers
ConsumerWidget is a widget whose build method receives both BuildContext and WidgetRef. WidgetRef is the object you use to interact with providers.
The application must place a ProviderScope above any widget that reads providers. ProviderScope creates the provider container that stores provider state.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
final greetingProvider = Provider<String>((ref) {
return 'Hello, Riverpod';
});
void main() {
runApp(const ProviderScope(child: MyApp()));
}
class MyApp extends ConsumerWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final greeting = ref.watch(greetingProvider);
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Provider example')),
body: Center(
child: Text(greeting),
),
),
);
}
}ref.watch subscribes the widget to the provider. If the provider produces a new value, Riverpod schedules this widget to rebuild. The value returned by watch is the provider's current value.
You can also use a Consumer when only part of a larger widget needs provider access:
class GreetingCard extends StatelessWidget {
const GreetingCard({super.key});
@override
Widget build(BuildContext context) {
return Consumer(
builder: (context, ref, child) {
final greeting = ref.watch(greetingProvider);
return Card(child: Text(greeting));
},
);
}
}Choose between watch, read, and listen
These methods have different purposes:
| Method | Subscribes to changes | Rebuilds the widget | Typical use |
|---|---|---|---|
| `ref.watch` | Yes | Yes | Values displayed by the UI |
| `ref.read` | No | No | One-time access in a callback |
| `ref.listen` | Yes | No | Side effects such as messages |
Use watch when the widget output depends on the provider. Use read for an event handler that needs the current value but should not rebuild the widget. Use listen when a change should trigger an action outside the widget's returned UI.
How the dependency works
A WidgetRef belongs to the widget's current build context. Do not keep it in a long-lived object. Pass provider dependencies through providers or use the ref only where Riverpod gives it to you.
Exercise
A widget displays a provider value, but it never updates when that value changes. Which WidgetRef method should you check first, and why?
Show answer
Check for ref.watch. It subscribes the widget to changes and causes the widget to rebuild with the new value. ref.read performs a one-time lookup and does not create that subscription.