AutoDisposeStateProvider<T> constructor

AutoDisposeStateProvider<T>(
  1. Create<T, AutoDisposeProviderReference> _create, {
  2. String? name,
})

A provider that expose a value which can be modified from outside.

It can be useful for very simple states, like a filter or the currently selected item – which can then be combined with other providers or accessed in multiple screens.

The following code shows a list of products, and allows selecting a product by tapping on it.

final selectedProductIdProvider = StateProvider<String?>((ref) => null);
final productsProvider = StateNotifierProvider<ProductsNotifier>((ref) => ProductsNotifier());

Widget build(BuildContext context, ScopedReader watch) {
  final List<Product> products = watch(productsProvider);
  final selectedProductId = watch(selectedProductIdProvider);

  return ListView(
    children: [
      for (final product in products)
        GestureDetector(
          onTap: () => selectedProductId.state = product.id,
          child: ProductItem(
            product: product,
            isSelected: selectedProductId.state == product.id,
          ),
        ),
    ],
  );
}

Implementation

AutoDisposeStateProvider(
  this._create, {
  String? name,
}) : super(name);