Skip to content

avoid_unnecessary_consumer_widgets

v0.1.0 Warning Fix Widget Best Practices

This rule detects ConsumerWidget subclasses where the WidgetRef parameter is never used inside the build method. If you are not reading or watching any providers, there is no reason to use ConsumerWidget over a plain StatelessWidget.

Every ConsumerWidget subscribes to the Riverpod container, which means it participates in the provider dependency graph even when it does not need to. Switching to StatelessWidget removes that overhead, makes the widget’s dependencies explicit (it has none), and signals to other developers that this widget is purely presentational.

See also: ConsumerWidget

// ConsumerWidget with unused ref parameter
class Greeting extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
// ref is never used
return Text('Hello');
}
}
// StatelessWidget since no providers are needed
class Greeting extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text('Hello');
}
}

To disable this rule:

plugins:
many_lints:
diagnostics:
avoid_unnecessary_consumer_widgets: false