Sub topic 7
Families: Turning Parameters into Provider Instances
Assume you know Dart functions, Future, Flutter widgets, and basic Riverpod usage with ProviderScope, ConsumerWidget, and ref.watch. A provider family is a provider with an input. Riverpod creates a separate provider instance for each input value.
Families are useful when the same loading or calculation logic applies to many values, such as user IDs, search terms, or product filters.
Add a parameter to create a family
With code generation, a function parameter after ref becomes a family parameter:
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'user.g.dart';
@riverpod
Future<User> user(Ref ref, int id) async {
final repository = ref.watch(userRepositoryProvider);
return repository.fetchUser(id);
}userProvider is now a family. Calling userProvider(42) selects the instance for user ID 42. Calling userProvider(7) selects a different instance.
In a widget, watch the selected instance:
class UserPage extends ConsumerWidget {
const UserPage({required this.userId, super.key});
final int userId;
@override
Widget build(BuildContext context, WidgetRef ref) {
final user = ref.watch(userProvider(userId));
return user.when(
loading: () => const CircularProgressIndicator(),
error: (error, stackTrace) => Text('Error: $error'),
data: (value) => Text(value.name),
);
}
}The family argument is part of the provider identity. Riverpod can therefore cache the result for each ID and notify only widgets that watch the affected instance.
A synchronous family works the same way:
@riverpod
String greeting(Ref ref, String name) {
return 'Hello, $name';
}
// ref.watch(greetingProvider('Mina'));The generated provider type follows the return type. A String return produces a synchronous provider. A Future<User> return produces an asynchronous provider.
Choose family arguments carefully
Riverpod identifies a family instance by comparing its arguments with ==. Primitive values such as int and String already have suitable equality. A custom argument class must define equality and hashCode from the fields that identify the request.
import 'package:flutter/foundation.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;
}The provider can now use the filter:
@riverpod
Future<List<Product>> products(
Ref ref,
ProductFilter filter,
) async {
final repository = ref.watch(productRepositoryProvider);
return repository.search(category: filter.category);
}Two ProductFilter(category: 'books') objects select the same family instance because their values are equal. Without the overrides, each newly created object could be treated as a different argument. That can cause repeated requests and unexpected cache entries.
Keep family arguments small and stable. Do not pass a widget, a mutable collection, or an object whose equality changes while it is being used. If a request needs several values, use an immutable value object.
Common family mistakes
A family call must include its argument:
final result = ref.watch(productsProvider(filter));Watching productsProvider without calling it refers to the family itself, not one selected instance.
Changing an argument creates or selects another instance. For example, each search query can have independent asynchronous state:
@riverpod
Future<List<Product>> search(Ref ref, String query) async {
final repository = ref.watch(productRepositoryProvider);
return repository.search(query: query);
}If the query changes from phone to tablet, the widget watches a different provider instance. Debounce rapidly changing input before starting network requests when the data source or user experience requires it.
Regenerate files after changing an annotation, parameter list, or return type:
dart run build_runner buildStale .g.dart files often produce errors that appear unrelated to the provider source.
Exercise
What happens when a widget changes from watching userProvider(42) to watching userProvider(7)?
Show answer
It switches to a different family instance, with its own identity and cached state.