ref property

WidgetRef ref
latefinal

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.

Implementation

late final WidgetRef ref = context as WidgetRef;