D
State ManagementIntermediate30 XP3 min read

What are Riverpod's .family and .autoDispose modifiers?

TL;DR: .family creates a parameterized provider (one per unique parameter). .autoDispose destroys the provider's state when no widget is listening — preventing memory leaks.

Full Answer

.family lets you create a provider that takes a parameter, like fetching a user by ID. Each unique parameter creates an independent provider instance.

.autoDispose tears down the provider (calls dispose on the notifier, cancels futures) when no listeners remain. Without it, providers live for the app's lifetime.

🎯

Combine: final userProvider = FutureProvider.autoDispose.family<User, String>((ref, userId) => api.fetchUser(userId)); This creates a per-user provider that cleans up when navigated away.

Code Examples

dart.family and .autoDispose combined
Output
Each userId gets its own cached Future; automatically disposed when the screen leaves the tree

Common Mistakes

  • Using .family with a complex object as parameter — requires overriding == and hashCode
  • Not using .autoDispose for per-screen providers — leads to memory leaks across navigations

Interview Tip

💡

The .autoDispose.family combination is the standard pattern for screen-specific async data. Knowing it signals practical Riverpod experience.

#Riverpod#family#autoDispose#provider-modifiers