Skip to main content

avoid-unnecessary-consumer-widgets

added in: 1.17.0

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

Example

❌ Bad:

// LINT
class HomeConsumerWidget extends ConsumerWidget {
const HomeConsumerWidget({super.key});


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

// LINT
class AnotherHomeConsumerWidget extends ConsumerWidget {
const AnotherHomeConsumerWidget();


Widget build(BuildContext context, WidgetRef ref) {
return Consumer(
builder: (context, ref, child) {
final value = ref.watch(helloWorldProvider);
return Text(value);
},
);
}
}

✅ Good:

class HomeConsumerWidget extends ConsumerWidget {
const HomeConsumerWidget();


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

return Text('$counter');
}
}