WidgetRef class sealed Core

An object that allows widgets to interact with providers.

WidgetRefs are typically obtained by using ConsumerWidget or its variants:

class Example extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // We now have a "ref"
  }
}

Once we have a WidgetRef, we can use its various methods to interact with providers. The most common use-case is to use WidgetRef.watch inside the build method of our widgets. This will enable our UI to update whenever the state of a provider changes:

@override
Widget build(BuildContext context, WidgetRef ref) {
  final count = ref.watch(counterProvider);
  // The text will automatically update whenever `counterProvider` emits a new value
  return Text('$count');
}

Note: Using a WidgetRef is equivalent to writing UI logic. As such, WidgetRefs should not leave the widget layer. If you need to interact with providers outside of the widget layer, consider using a Ref instead.

Implemented types

Properties

container ProviderContainer
The ProviderContainer that is used to run the mutation.
no setterinherited
context BuildContext
The BuildContext of the widget associated to this WidgetRef.
no setter
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

exists(ProviderBase<Object?> provider) bool
Determines whether a provider is initialized or not.
invalidate(ProviderOrFamily provider, {bool asReload = false}) → void
Invalidates the state of the provider, causing it to refresh.
listen<StateT>(ProviderListenable<StateT> provider, void listener(StateT? previous, StateT next), {void onError(Object error, StackTrace stackTrace)?}) → void
Listen to a provider and call listener whenever its value changes, without having to take care of removing the listener.
listenManual<StateT>(ProviderListenable<StateT> provider, void listener(StateT? previous, StateT next), {void onError(Object error, StackTrace stackTrace)?, bool fireImmediately}) ProviderSubscription<StateT>
Listen to a provider and call listener whenever its value changes.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
read<StateT>(ProviderListenable<StateT> provider) → StateT
Reads a provider without listening to it.
refresh<StateT>(Refreshable<StateT> provider) → StateT
Forces a provider to re-evaluate its state immediately, and return the created value.
toString() String
A string representation of this object.
inherited
watch<StateT>(ProviderListenable<StateT> provider) → StateT
Returns the value exposed by a provider and rebuild the widget when that value changes.

Operators

operator ==(Object other) bool
The equality operator.
inherited