Sub topic 12
Parameterized providers with `family`
This chapter assumes Dart generics, Future, async/await, Flutter widget basics, and the difference between ref.watch and ref.read. You will use those ideas to create provider instances based on an input value.
Why use a provider family?
A family creates a separate provider state for each argument. This is useful when the same operation must run for different values, such as:
- Loading a user by ID.
- Loading search results for a query.
- Loading the messages in a conversation.
- Checking permissions for a particular document.
Without a family, you might create one provider for every possible ID. A family lets one declaration describe all of those providers.
final userNameProvider =
FutureProvider.autoDispose.family<String, int>((ref, userId) async {
await Future<void>.delayed(const Duration(milliseconds: 300));
return 'User $userId';
});The type parameters mean:
| Part | Meaning |
|---|---|
| `String` | The value produced by each provider instance |
| `int` | The type of the family argument |
| `userId` | The argument received by the provider callback |
Calling userNameProvider(42) creates the provider instance for user 42. Calling userNameProvider(7) creates a different instance with separate state.
Reading a family provider
A widget passes the argument when it watches the provider:
class UserNameLabel extends ConsumerWidget {
const UserNameLabel({
super.key,
required this.userId,
});
final int userId;
@override
Widget build(BuildContext context, WidgetRef ref) {
final userName = ref.watch(userNameProvider(userId));
return userName.when(
data: Text.new,
loading: () => const CircularProgressIndicator(),
error: (error, stackTrace) => Text('Error: $error'),
);
}
}The widget watches only the instance for its userId. If another widget watches userNameProvider(7), it does not receive the state for userNameProvider(42).
The argument should have stable equality. Primitive values such as int, String, and enum work well. If you pass a custom object, implement value equality so Riverpod can recognize equivalent arguments.
Controlling lifetime with `autoDispose`
A normal provider can keep its state while the container exists. autoDispose allows Riverpod to discard a provider instance after it has no listeners. This is useful for temporary screens, search queries, and detail pages.
When a widget leaves the screen, an auto-disposed family instance can be released. Later, watching the same argument may run the provider again.
A common mistake is passing an unstable argument, such as creating a new object during every build. That can produce unnecessary provider instances. Prefer an ID, string, or immutable value object.
Choosing a family design
Use a family when the operation is the same but its input changes. Use separate providers when the operations have different behavior or different dependencies. Keep the family argument small and meaningful. If an operation needs several values, define an immutable parameter class with correct equality or use a Dart record.
Exercise
What is the difference between userNameProvider(42) and userNameProvider(7), and when can autoDispose release their state?
Show answer
They are separate family instances with separate state. autoDispose can release an instance after it has no listeners, such as when the screen watching it is removed.