Sub topic 1
Why test providers with a ProviderContainer
Riverpod providers contain application logic, state, and dependency wiring. Testing that logic through a widget can work, but it adds Flutter concerns such as rendering, widget lifecycles, and frame timing. A ProviderContainer gives providers a small, non-visual environment for a plain Dart test.
This chapter assumes you know basic Dart testing and Riverpod provider types. The examples use flutter_test, but the same approach works with the test package when your project does not need Flutter bindings.
Prerequisites
You should be comfortable with:
- Dart classes, methods, and
Future test,expect, andaddTearDown- Riverpod providers and notifiers
- The difference between
ref.watchandref.read
The provider graph in a test
A provider can depend on other providers. Together, those providers form a provider graph. In a Flutter app, a ProviderScope creates the environment that widgets use to access this graph. In a plain Dart test, ProviderContainer plays that role.
The container:
- Creates provider state when a provider is first read or watched
- Resolves dependencies between providers
- Stores state for the lifetime of the container
- Lets the test read values and call notifier methods
- Can replace dependencies with test-specific overrides
- Disposes resources when the test ends
The test does not need to build a widget or start a Flutter application.
A useful mental model is that the container is both a dependency-injection scope and a state store. Each test should normally get a new scope so that state from one test cannot affect another.
A first container test
Consider a notifier that owns an integer:
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
class Counter extends Notifier<int> {
@override
int build() => 0;
void increment() => state++;
}
final counterProvider = NotifierProvider<Counter, int>(Counter.new);
void main() {
test('the counter starts at zero and increments', () {
final container = ProviderContainer();
addTearDown(container.dispose);
expect(container.read(counterProvider), 0);
container.read(counterProvider.notifier).increment();
expect(container.read(counterProvider), 1);
});
}ProviderContainer() creates an isolated provider environment. The first container.read(counterProvider) creates the notifier and returns its current state.
The .notifier property exposes the notifier instance rather than its state. This lets the test call public methods such as increment. The second read verifies the state after the method changes it.
addTearDown(container.dispose) is important. Disposal releases provider resources and prevents subscriptions or other lifecycle work from leaking into later tests. Register it immediately after creating the container, so every test path cleans up reliably.
Why this is better than testing only through widgets
Widget tests answer questions about the integration between providers and the UI. Container tests answer questions about provider behavior itself.
A container test is usually:
- Faster because it does not build a widget tree
- More focused because it checks state and provider behavior directly
- Easier to diagnose because failures are separate from layout and rendering
- Suitable for repositories, notifiers, derived providers, and asynchronous providers
This does not make widget tests unnecessary. If a widget displays loading, error, or data states incorrectly, that behavior belongs in a widget test. A practical test suite uses container tests for provider logic and widget tests for UI integration.
Isolating each test
Avoid storing a container in a global variable or sharing one container across tests. Shared containers preserve provider state and can make test order affect results.
Create a fresh container inside each test:
- Construct the container.
- Register
container.disposewithaddTearDown. - Read or listen to the providers under test.
- Assert the expected behavior.
This structure also prepares the test for overrides. Later, a test can construct the same container with fake repositories or other dependencies without changing the production provider definitions.
Exercise
Why should a test create and dispose its own ProviderContainer?
Show answer
A fresh container isolates provider state, while disposal releases resources and prevents subscriptions or lifecycle work from leaking into other tests.