What is ProxyProvider used for in the Provider package?
Short Answer
ProxyProvider lets you create a provider whose value depends on one or more other providers above it — for example, building a repository that needs an already-provided API client.
A plain Provider creates a value independently. ProxyProvider<ApiClient, UserRepository>((context, apiClient, previous) => UserRepository(apiClient)) instead builds its value from another provider's value, re-running the builder whenever the upstream ApiClient provider changes. This is how you express a dependency chain declaratively: AuthProvider → ApiClient → UserRepository, wired together via nested/MultiProvider ProxyProviders.
The previous parameter lets you reuse or dispose of the prior value manually if needed, which matters for resources that shouldn't be recreated on every rebuild unless their actual dependency changed.
Code Example
MultiProvider(
providers: [
Provider<ApiClient>(create: (_) => ApiClient()),
ProxyProvider<ApiClient, UserRepository>(
update: (context, apiClient, previous) => UserRepository(apiClient),
),
],
child: const MyApp(),
)Common Mistakes
- ×Reaching for ProxyProvider chains when Riverpod's simpler ref.watch(otherProvider) would express the same dependency more directly.
- ×Not disposing a previous resource when it's replaced, if it holds something needing explicit cleanup.