StateProvider<ValueT> class
final
A provider that exposes a value that 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, List<Product>>((ref) => ProductsNotifier());
Widget build(BuildContext context, WidgetRef ref) {
final List<Product> products = ref.watch(productsProvider);
final selectedProductId = ref.watch(selectedProductIdProvider);
return ListView(
children: [
for (final product in products)
GestureDetector(
onTap: () => ref.read(selectedProductIdProvider.notifier).state = product.id,
child: ProductItem(
product: product,
isSelected: selectedProductId.state == product.id,
),
),
],
);
}
- Inheritance
-
- Object
- ProviderOrFamily
- ProviderBase<
ValueT> - StateProvider
- Available extensions
- Annotations
-
- @publicInLegacy
Constructors
-
StateProvider.new(ValueT _createFn(Ref ref), {String? name, Iterable<
ProviderOrFamily> ? dependencies, bool isAutoDispose = false, Retry? retry}) - A provider that exposes a value that can be modified from outside.
Properties
- argument → Object?
-
If this provider was created with the
.family
modifier, argument is the variable that was used.finalinherited -
dependencies
→ Iterable<
ProviderOrFamily> ? -
The list of providers that this provider potentially depends on.
finalinherited
- from → Family?
-
If this provider was created with the
.family
modifier, from is the.family
instance.finalinherited - hashCode → int
-
The hash code for this object.
no setterinherited
- isAutoDispose → bool
-
Whether the state associated to this provider should be disposed
automatically when the provider stops being listened.
finalinherited
- name → String?
-
A custom label for providers.
finalinherited
-
notifier
→ Refreshable<
StateController< ValueT> > -
Obtains the StateController of this provider.
no setter
- retry → Retry?
-
The retry strategy to use when a provider fails.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
create(
Ref ref) → ValueT - @nodoc
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
overrideWith(
Create< ValueT> create) → Override -
Override the provider with a new initialization function.
inherited
-
select<
OutT> (OutT selector(InT value)) → ProviderListenable< OutT> -
Available on ProviderListenable<
Partially listen to a provider.InT> , provided by the ProviderListenableSelect extension -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- autoDispose → const AutoDisposeStateProviderBuilder
- Marks the provider as automatically disposed when no longer listened to.
- family → const StateProviderFamilyBuilder
- A group of providers that builds their value from an external parameter.