Sub topic 9
Keeping Auto-Dispose Providers Alive in a Test
This chapter assumes you know Dart tests, addTearDown, ProviderContainer, and basic Riverpod providers. It also assumes you understand that autoDispose providers release their state when no longer needed.
An auto-dispose provider is useful when its state should not remain in memory after the provider is unused. In a test, however, this behavior can happen earlier than you expect. A test that only calls container.read may lose the provider state before it finishes checking it.
Why Auto-Dispose Changes Test Behavior
A normal provider remains available in the container until the container is disposed. An auto-dispose provider can be destroyed after it has no listeners.
A listener is code that subscribes to provider changes. A widget usually creates this kind of subscription through ref.watch. In a plain Dart test, container.listen creates the subscription explicitly.
The subscription keeps the provider alive:
Hold a Listener for the Test
Consider a notifier that stores a counter:
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
class Counter extends AutoDisposeNotifier<int> {
@override
int build() => 0;
void increment() => state++;
}
final autoCounterProvider =
AutoDisposeNotifierProvider<Counter, int>(Counter.new);
void main() {
test('keeps auto-dispose state alive while listening', () {
final container = ProviderContainer();
addTearDown(container.dispose);
final subscription = container.listen<int>(
autoCounterProvider,
(previous, next) {},
);
container.read(autoCounterProvider.notifier).increment();
expect(container.read(autoCounterProvider), 1);
subscription.close();
});
}container.listen returns a ProviderSubscription. The subscription remains open while the test uses the provider. Calling subscription.close() removes the listener. After that, Riverpod may dispose the provider when its auto-dispose rules allow it.
The listener callback is empty because this test does not need to record changes. Its purpose is to keep the provider active. If you need to verify every state change, collect values in the callback instead.
Read Versus Listen
container.read gets a value once. It does not express that the test needs to observe the provider for a period of time.
container.listen establishes an ongoing relationship with the provider. It is the safer choice when the test:
- Changes state and reads it later.
- Checks several state transitions.
- Tests behavior that depends on the provider remaining mounted.
- Needs to verify that a provider emits particular values.
| Operation | Purpose | Keeps an auto-dispose provider active |
|---|---|---|
| `container.read(provider)` | Get the current value once | No lasting subscription |
| `container.listen(provider, callback)` | Observe value changes | Yes, until the subscription closes |
A test can use both operations. Use listen to keep the provider alive and read to inspect its current state.
Test Disposal and Reset Behavior
A test should still dispose its ProviderContainer with addTearDown, even when it closes individual subscriptions. The container owns provider state and other resources created during the test.
You can also test that an auto-dispose provider starts fresh after its listener is closed. The exact disposal timing depends on Riverpod's lifecycle scheduling, so avoid assertions that depend on immediate disposal in the same synchronous statement. If reset behavior matters, let the event loop advance and then create a new subscription or read the provider again.
The important distinction is this:
- Closing a subscription permits disposal.
- Disposing the container guarantees cleanup for the whole test.
A Practical Pattern
For a provider that must remain alive throughout a test, use this sequence:
- Create a new
ProviderContainer. - Register
container.disposewithaddTearDown. - Start a listener with
container.listen. - Perform reads and notifier actions.
- Close the subscription when the test no longer needs it.
Exercise
Why might container.read(autoCounterProvider) fail to preserve the provider's state for the rest of a test?
Show answer
read retrieves the current value but does not create a lasting listener. An auto-dispose provider can be disposed when it has no active listeners, so the test should keep a container.listen subscription open.