Skip to content

avoid_build_context_in_providers

v0.8.0 Warning Riverpod State

Flags a BuildContext parameter on a @riverpod provider — either a functional provider or a method of a @riverpod class.

Providers outlive the widgets that read them. A BuildContext held by a provider can easily refer to a widget that has already been unmounted, and using it then throws dependOnInheritedWidgetOfExactType was called on a defunct widget — or quietly reads stale inherited data. Passing the value you need instead keeps the provider independent of the widget tree, which is also what makes it testable without pumping a widget.

See also: Riverpod families

@riverpod
int example(Ref ref, BuildContext context) => 0; // LINT
@riverpod
class Example extends _$Example {
@override
int build(BuildContext context) => 0; // LINT
}
// Pass the value read from the context, not the context itself.
@riverpod
int example(Ref ref, Locale locale) => 0;
// At the call site:
ref.watch(exampleProvider(Localizations.localeOf(context)));

Ordinary classes and functions are unaffected — only @riverpod declarations are checked:

int helper(BuildContext context) => 0; // no lint

This rule is in the recommended preset, so it is on with preset: recommended or preset: opinionated. Add it to preset: core with avoid_build_context_in_providers: true.

To turn it off:

many_lints.yaml
rules:
avoid_build_context_in_providers: false

To keep the rule on but skip certain paths, use per-rule exclude.