Sub topic 8
Keeping family arguments equal and stable
Assume you know Dart classes, ==, hashCode, and basic Riverpod usage with ProviderScope, ConsumerWidget, and ref.watch. This chapter focuses on one family rule: equal arguments must identify the same provider instance.
A family is a group of provider instances created from one provider definition. For example, userProvider(42) and userProvider(7) are two instances of the same family. Riverpod caches each instance separately.
How Riverpod identifies a family instance
Riverpod uses the family argument when it looks up an instance. For simple values, Dart already has the expected equality behavior:
- Two equal
intvalues compare equal. - Two equal
Stringvalues compare equal. nullcompares equal tonull.
A generated provider with a parameter becomes a family:
@riverpod
Future<User> user(Ref ref, int id) async {
final repository = ref.watch(userRepositoryProvider);
return repository.fetchUser(id);
}userProvider(42) and userProvider(7) have different arguments, so they have separate loading, error, and data states. Calling userProvider(42) again addresses the cached instance for 42.
The argument is compared with Dart's == operator. This matters when you pass a custom object.
Why a custom object can create duplicate instances
Dart classes use identity equality by default. Two separately created objects are therefore different, even when their fields contain the same values:
final first = ProductFilter(category: 'books');
final second = ProductFilter(category: 'books');
print(first == second); // false without custom equalityIf you use these objects as family arguments, Riverpod can treat each object as a new family instance. Rebuilding a widget might then create another provider instance for the same logical filter. This can cause repeated requests, lost cached state, and unnecessary memory use.
Define value equality
A family argument should usually be an immutable value object. Override both == and hashCode using the fields that define its meaning.
import 'package:meta/meta.dart';
@immutable
class ProductFilter {
const ProductFilter({required this.category});
final String category;
@override
bool operator ==(Object other) =>
other is ProductFilter && other.category == category;
@override
int get hashCode => category.hashCode;
}Now two filters with the same category are equal:
const first = ProductFilter(category: 'books');
const second = ProductFilter(category: 'books');
print(first == second); // true
print(first.hashCode == second.hashCode); // trueThe hashCode rule is important: equal objects must have equal hash codes. If you override == but not hashCode, collections and provider caches can behave incorrectly.
Use the value object in a family like this:
@riverpod
Future<List<Product>> products(Ref ref, ProductFilter filter) async {
final repository = ref.watch(productRepositoryProvider);
return repository.search(category: filter.category);
}
// Equal filters select the same family instance.
final filterA = const ProductFilter(category: 'books');
final filterB = const ProductFilter(category: 'books');
ref.watch(productsProvider(filterA));
ref.watch(productsProvider(filterB));The generated family receives the two filters as equal arguments, so both calls address the same cached provider state.
Keep arguments immutable
Equality is safe only while the fields used by equality stay unchanged. If an argument changes after Riverpod caches it, the object can become difficult to find in a cache.
Prefer:
finalfields.constconstructors where possible.- Strings, numbers, enums, and other immutable values.
- New objects when a filter needs to change.
Avoid using a mutable List, Map, or Set directly in a family argument. Their contents can change without changing the object identity, and standard list equality is identity-based rather than element-based. Convert mutable input into an immutable value representation, or implement equality for a dedicated value type.
A quick diagnostic checklist
When a family reloads unexpectedly, check:
- Does the argument type override both
==andhashCode? - Are all equality fields immutable?
- Are lists or maps compared by their contents?
- Does the widget create an equivalent value instead of an identity-only object?
- Are you intentionally changing the argument?
Exercise
A widget calls productsProvider(ProductFilter(category: 'books')) on every build. The filter class has no == or hashCode override. What problem can this cause, and what is the fix?
Show answer
Each new filter object can select a different family instance, causing repeated work and separate cached states. Make the filter immutable and implement value equality with a matching hashCode, or pass stable primitive arguments.