Sub topic 3
Reading Provider State
This chapter assumes you know Dart and Flutter basics, can write package:test or flutter_test tests, and understand basic Riverpod providers. You should also know the difference between ref.watch and ref.read, and be comfortable with Future and async/await.
A ProviderContainer is the Riverpod test harness for plain Dart tests. It stores provider state and resolves provider dependencies without building a widget tree. You use container.read to obtain a provider's current value.
Reading a synchronous provider
Create a new container in each test. Then read the provider through that container.
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
final greetingProvider = Provider<String>((ref) => 'Hello, Riverpod');
void main() {
test('reads a provider value', () {
final container = ProviderContainer();
addTearDown(container.dispose);
final greeting = container.read(greetingProvider);
expect(greeting, 'Hello, Riverpod');
});
}container.read(greetingProvider) returns the value produced by greetingProvider. Riverpod also creates any providers that the provider depends on.
The container owns the provider elements and their cached state. Calling dispose releases that state and any resources used by providers. addTearDown guarantees cleanup even when an assertion fails.
Reading a notifier's state
A notifier is an object that owns mutable provider state. With a NotifierProvider, read the state by passing the provider itself. Read the notifier object by using .notifier.
class Counter extends Notifier<int> {
@override
int build() => 0;
void increment() => state++;
}
final counterProvider = NotifierProvider<Counter, int>(Counter.new);
void main() {
test('reads state before and after a notifier method', () {
final container = ProviderContainer();
addTearDown(container.dispose);
expect(container.read(counterProvider), 0);
final counter = container.read(counterProvider.notifier);
counter.increment();
expect(container.read(counterProvider), 1);
});
}The first read asks for the exposed state, which is an int. The second read asks for the Counter instance. Calling increment changes the notifier's state, and reading counterProvider again returns the new value.
Do not call a notifier method through a widget reference in a plain provider test. The container is the object that connects the test to Riverpod's dependency graph.
`read` and `watch` have different jobs
Inside a provider, ref.watch declares a dependency. If the watched provider changes, Riverpod can recompute the current provider. In a test, container.read takes a snapshot of the value at the moment of the call. It does not register a callback for later changes.
| Operation | Where it is commonly used | Effect |
|---|---|---|
| `ref.watch(provider)` | Provider definitions and widgets | Establishes a reactive dependency |
| `ref.read(provider)` | Tests and event handlers | Gets the current value once |
| `container.read(provider)` | Plain Dart tests | Gets provider state from a test container |
| `container.read(provider.notifier)` | Tests | Gets the notifier to call its methods |
If the test must verify every transition, use container.listen instead of repeatedly polling with read. Listening is covered later, but the distinction matters here: reading answers “what is the value now?”, while listening answers “what values were emitted?”
Reading asynchronous state
A FutureProvider exposes an AsyncValue<T>. The value can be loading, data, or error. To wait for a successful result in a test, read the provider's .future.
final userNameProvider = FutureProvider<String>((ref) async {
return 'Ada';
});
void main() {
test('reads the completed FutureProvider value', () async {
final container = ProviderContainer();
addTearDown(container.dispose);
final name = await container.read(userNameProvider.future);
expect(name, 'Ada');
});
}container.read(userNameProvider) gives an AsyncValue<String> immediately. container.read(userNameProvider.future) gives a Future<String> that completes with the data or completes with an error. Await that future before making assertions.
Exercise
Which expression should a test use to call increment on the counter notifier, and which expression should it use to read the counter's integer state?
Show answer
Use container.read(counterProvider.notifier).increment() to call the method, and use container.read(counterProvider) to read the integer state.