Skip to main content

avoid-unnecessary-consumer-widgets

added in: 1.17.0
Pro+

Warns when a ConsumerWidget has an unused ref and can be converted to a regular widget.

A consumer widget that does not use refs can be converted to a regular widget which will simplify your code.

Example

❌ Bad:

// LINT: This widget does not use its ref and can be converted to a regular widget.
class HomeConsumerWidget extends ConsumerWidget {
const HomeConsumerWidget({super.key});


Widget build(BuildContext context, WidgetRef ref) {
return Text('$counter');
}
}

// LINT: This widget does not use its ref and can be converted to a regular widget.
class AnotherHomeConsumerWidget extends ConsumerWidget {
const AnotherHomeConsumerWidget();


Widget build(BuildContext context, WidgetRef ref) {
return Consumer(
builder: (context, ref, child) {
// This ref is from the 'Consumer' widget
final value = ref.watch(helloWorldProvider);
return Text(value);
},
);
}
}

✅ Good:

// Correct, actually uses 'ref'
class HomeConsumerWidget extends ConsumerWidget {
const HomeConsumerWidget();


Widget build(BuildContext context, WidgetRef ref) {
final counter = ref.watch(counterProvider);

return Text('$counter');
}
}