Advertisement
Beginner Flutter, State Management with Riverpod

How does ConsumerWidget let a StatelessWidget properly and reactively access and correctly rebuild in response to a given Riverpod provider's own current value?

Short answer

ConsumerWidget replaces a regular StatelessWidget's own build method with one that instead receives an additional WidgetRef parameter, letting that particular widget properly call ref.watch directly to reactively subscribe to a given specific provider, automatically causing that…

Save Flutter mock interview
Advertisement
AModel answer
ConsumerWidget replaces a regular StatelessWidget's own build method with one that instead receives an additional WidgetRef parameter, letting that particular widget properly call ref.watch directly to reactively subscribe to a given specific provider, automatically causing that particular widget to properly rebuild itself whenever that specific watched provider's own value actually genuinely changes, providing a genuinely clean, considerably declarative way to properly connect your interface directly to your app's own underlying Riverpod managed state.
class CounterText extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final count = ref.watch(counterProvider);
    return Text('Count: $count');
  }
}
Real-world example
A counter display widget extends ConsumerWidget rather than a regular StatelessWidget, letting it properly watch the counterProvider and automatically rebuild itself the very instant that provider's own underlying value actually genuinely changes.
Likely follow-ups
  • What is the specific practical difference between ConsumerWidget and simply wrapping a regular widget with a Consumer widget instead?
  • How does ConsumerStatefulWidget genuinely differ from ConsumerWidget?
Related topics