ConsumerWidget constructor Null safety

const ConsumerWidget(
  1. {Key? key}
)

A StatelessWidget that can listen to providers.

Using ConsumerWidget, this allows the widget tree to listen to changes on provider, so that the UI automatically updates when needed.

Do not modify any state or start any http request inside build.

As a usage example, consider:

final helloWorldProvider = Provider((_) => 'Hello world');

We can then subclass ConsumerWidget to listen to helloWorldProvider like so:

class Example extends ConsumerWidget {
  const Example({Key? key}): super(key: key);

  @override
  Widget build(BuildContext context, ScopedReader watch) {
    final value = watch(helloWorldProvider);
    return Text(value); // Hello world
  }
}

Note You can watch as many providers inside build as you want to:

@override
Widget build(BuildContext context, ScopedReader watch) {
  final value = watch(someProvider);
  final another = watch(anotherProvider);
  return Text(value); // Hello world
}

For reading providers inside a StatefulWidget or for performance optimizations, see Consumer.

Implementation

const ConsumerWidget({Key? key}) : super(key: key);