lookup_through_outer_context
Reports a lookup through a BuildContext above the widget it means, because the same code creates that widget around the lookup. Look it up through a context below the widget, such as a Builder’s, or use a value already at hand.
- Tier
- blocking
- Scope
- Flutter
- Lowest preset
- recommended
- Fix
- none
What it reports
Section titled “What it reports”The violation is on the lookup, and the message names the widget:
Widget build(BuildContext context) => Scaffold( body: TextButton( onPressed: () => Scaffold.of(context).openDrawer(), child: const Text('Menu'), ),);The lookups are:
X.of(context)andX.maybeOf(context)for a classX, which mean a widget ofXor a subclass;dependOnInheritedWidgetOfExactType,getInheritedWidgetOfExactTypeandfindAncestorWidgetOfExactTypeof the context, which mean a widget of the type argument;- for a provided value of a type
T:of<T>(context)of a class whose name ends inProvider, andread<T>,watch<T>andselect<T, …>of an extension on the context declared outside Flutter. A provider meansTwhen it is created withTamong its type arguments, or holds such a provider in itsproviderslist.
The context must be a parameter of the code (the build method, a builder) or the context of the State. The widget must be created inside that code, with the lookup in one of its arguments that hold widgets: a widget, a list of widgets, or a function that builds one.
It does not report:
- a lookup through the context of a builder inside the widget:
Builder(builder: (inner) => …Scaffold.of(inner)…); - a lookup in an argument that holds no widgets, such as
create:ordata:; - a context held in a local variable;
- a widget made from what the context sees, which changes it on purpose: one given the context (
MediaQuery.removePadding(context: context, …)), or one with the same lookup in another argument (Theme(data: Theme.of(context).copyWith(…), …)); - a lookup through the outer context in a builder whose own context goes unused, with no widget of the lookup between: both contexts give the same answer there.
A lookup searches upwards from the context’s widget, which is above everything its build creates, so it cannot find the widget created right there. It finds another one further up (the page’s Scaffold for a nested one, the app’s Theme inside a Theme that changes it), or none and throws: Scaffold.of() called with a context that does not contain a Scaffold, ProviderNotFoundException.
Example
Section titled “Example”Widget build(BuildContext context) => BlocProvider( create: (_) => CounterCubit(), child: TextButton( onPressed: () => context.read<CounterCubit>().increment(), child: const Text('+'), ),);Widget build(BuildContext context) => BlocProvider( create: (_) => CounterCubit(), child: Builder( builder: (context) => TextButton( onPressed: () => context.read<CounterCubit>().increment(), child: const Text('+'), ), ),);Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// Why it stays as written here.// ignore: lookup_through_outer_context// ignore_for_file: assay/lookup_through_outer_context silences it in a whole file, and // ignore: assay/lookup_through_outer_context with the prefix works too. See Suppressions.