Sub topic 2
One Container per Test, Disposed with `addTearDown`
This chapter assumes you know Dart and Flutter basics, can write tests with flutter_test, and understand basic Riverpod providers. You should also know the difference between ref.watch and ref.read, and understand Dart Future and async code.
Why a Test Needs Its Own `ProviderContainer`
A ProviderContainer is the object that stores and evaluates Riverpod provider state outside a widget tree. In a widget test, ProviderScope creates this environment for you. In a plain Dart test, you create the container yourself.
A separate container gives each test an isolated provider graph. The graph is the set of providers and their current state that the container manages.
This isolation matters when a provider stores mutable state. If several tests share one container, one test can change state that another test later reads. The test order may then affect the result.
The basic lifecycle is:
Create and Dispose the Container
Create the container inside the test. Immediately register its dispose method with addTearDown.
addTearDown registers cleanup that runs after the test finishes. It runs even when an assertion fails, so the container is not left running after a failed test.
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 greeting', () {
final container = ProviderContainer();
addTearDown(container.dispose);
expect(container.read(greetingProvider), 'Hello, Riverpod');
});
}The test creates a new container every time it runs. The container.read call evaluates greetingProvider and returns its current value.
Disposal releases resources owned by the container. This includes provider state and lifecycle work associated with providers. It is especially important when providers create streams, timers, subscriptions, or other objects that need cleanup.
Why Shared Containers Cause Failures
A shared container makes tests depend on hidden state. Consider a notifier that starts at zero and can increment its state:
class Counter extends Notifier<int> {
@override
int build() => 0;
void increment() => state++;
}
final counterProvider = NotifierProvider<Counter, int>(Counter.new);If one test increments counterProvider and another test uses the same container, the second test may see 1 instead of 0. A fresh container prevents this:
void main() {
test('starts at zero', () {
final container = ProviderContainer();
addTearDown(container.dispose);
expect(container.read(counterProvider), 0);
});
test('can be changed independently', () {
final container = ProviderContainer();
addTearDown(container.dispose);
container.read(counterProvider.notifier).increment();
expect(container.read(counterProvider), 1);
});
}Each test has its own instance of the provider state. The order of these tests does not matter.
Avoid storing a container in a top-level variable unless you have a specific reason and carefully reset it. A top-level container usually creates more lifecycle work and makes test isolation harder to see.
Cleanup with `addTearDown`
addTearDown accepts a callback. The callback can be synchronous or asynchronous. For a ProviderContainer, passing container.dispose is enough:
final container = ProviderContainer();
addTearDown(container.dispose);You can register additional cleanup when the test creates another resource:
test('cleans up all test resources', () {
final container = ProviderContainer();
addTearDown(container.dispose);
final subscription = container.listen(counterProvider, (_, __) {});
addTearDown(subscription.close);
expect(container.read(counterProvider), 0);
});Register cleanup in the same test that creates the resource. This keeps ownership clear and reduces the chance of leaking state between tests.
Exercise
Why should each test create and dispose its own ProviderContainer?
Show answer
A new container isolates provider state from other tests, and disposing it releases provider resources after the test finishes.