Identify providers and manage their state

Use args to identify a provider, Scope to pass dependencies, and clear rules to decide when state is cleaned up.

How this works with familiar Riverpod ideas

Use a subclass with args to define a family-like provider. Call invoke explicitly when you want to run an update.

What identifies a provider

runtimeType and a hash of args

Create a family-like provider

Extend Provider or AsyncProvider and call super.args((...)).

Get the default dependency

Scope<T>.required + overrideWithValue

Why constructors help

Ordinary Dart constructors make it easier to see why two providers match and which instance an override will replace. That helps when writing tests or tracing a problem.

How args identify a provider

args help identify a provider. Within one ProviderContainer, equal args for the same provider type use the same cached value.

What this means for your code

- Define a subclass with args to get family-like behavior.
- Override individual provider instances for the arguments you need.
- Keep args immutable so caching stays predictable.

Build a family-like provider with a default from Scope

Choose constructor arguments that distinguish one instance from another, then supply a default dependency through Scope.

class ProductProvider extends AsyncProvider<List<Product>> {
  ProductProvider({this.search = ''}) : super.args((search,));
  final String search;

  static final fallback = Scope<ProductProvider>.required('product.fallback');

  @override
  FutureOr<List<Product>> build(ref) async {
    final api = ref.watch(productsApiProvider);
    return api.search(q: search);
  }
}

// Inject
ProviderScope(
  overrides: [
    ProductProvider.fallback.overrideWithValue(ProductProvider(search: 'jeans')),
  ],
  child: const App(),
);
Use Scope to make it clear where a dependency comes from and to make it easier to replace in tests.
Call overrideWithValue for the provider instance you want to replace. This works for instances with args, too.
autoDispose works the same way for providers defined with a subclass and args.

Where to go next

Read provider state

Try watch, read, and listen, and look up when to use AsyncProvider.future.

Learn to read provider state

Run mutations

Try a mutation token, define a write with mutate, and run it through ref.invoke.

Learn to run mutations