Sub topic 4
Testing a Notifier's Methods and State
This chapter assumes you know Dart, flutter_test, and basic Riverpod. You should already understand NotifierProvider, Notifier, and the difference between ref.watch and ref.read.
A Notifier is a Riverpod class that owns mutable state and exposes methods that change that state. Provider tests can call those methods directly. They do not need a widget, WidgetTester, or ProviderScope. A ProviderContainer is the test harness that builds providers and stores their state.
Create one container for each test
Create a fresh ProviderContainer inside every test. Register its disposal with addTearDown immediately after creating it.
Disposing the container releases provider state and other resources. A fresh container also prevents one test from affecting another test.
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('counter starts at zero', () {
final container = ProviderContainer();
addTearDown(container.dispose);
expect(container.read(counterProvider), 0);
});
}container.read(counterProvider) returns the provider's current value. In this example, the value is an int, so the initial state is 0.
The provider is usually created lazily. Reading it causes Riverpod to construct the Counter and run its build method.
Call notifier methods through the provider
To call a notifier method, read the provider's .notifier:
void main() {
test('increment updates state', () {
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 two provider reads have different types:
container.read(counterProvider)reads the state, which is anint.container.read(counterProvider.notifier)reads theCounterobject, so the test can callincrement.
Testing both the initial value and the result after a method call checks the notifier's public behavior. It also avoids testing private implementation details.
A notifier can expose several methods. Test each meaningful state transition separately.
class Counter extends Notifier<int> {
@override
int build() => 0;
void increment() => state++;
void decrement() => state--;
void reset() => state = 0;
}A test can call multiple methods when their sequence is part of the behavior being verified:
test('reset returns the counter to zero', () {
final container = ProviderContainer();
addTearDown(container.dispose);
final counter = container.read(counterProvider.notifier);
counter.increment();
counter.increment();
expect(container.read(counterProvider), 2);
counter.reset();
expect(container.read(counterProvider), 0);
});Understand state replacement
The state property represents the current value owned by the notifier. Assigning a new value publishes that value to Riverpod. For immutable state objects, replace the entire object instead of mutating it in place.
For example:
class Settings extends Notifier<({bool darkMode, String language})> {
@override
({bool darkMode, String language}) build() {
return (darkMode: false, language: 'en');
}
void enableDarkMode() {
state = (darkMode: true, language: state.language);
}
}
final settingsProvider =
NotifierProvider<Settings, ({bool darkMode, String language})>(
Settings.new,
);The test reads the state before and after the method:
test('enableDarkMode preserves the language', () {
final container = ProviderContainer();
addTearDown(container.dispose);
container.read(settingsProvider.notifier).enableDarkMode();
expect(container.read(settingsProvider), (darkMode: true, language: 'en'));
});This assertion checks both effects of the method: dark mode changes, and the unrelated language value remains unchanged.
Exercise
Exercise
Write a test that creates a fresh container, calls decrement once on a counter that starts at zero, and verifies that the state is -1.
Show answer
Use container.read(counterProvider.notifier).decrement() and then assert expect(container.read(counterProvider), -1). Register addTearDown(container.dispose) after creating the container.