Skip to content

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

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) and X.maybeOf(context) for a class X, which mean a widget of X or a subclass;
  • dependOnInheritedWidgetOfExactType, getInheritedWidgetOfExactType and findAncestorWidgetOfExactType of 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 in Provider, and read<T>, watch<T> and select<T, …> of an extension on the context declared outside Flutter. A provider means T when it is created with T among its type arguments, or holds such a provider in its providers list.

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: or data:;
  • 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.

Violating
Widget build(BuildContext context) => BlocProvider(
create: (_) => CounterCubit(),
child: TextButton(
onPressed: () => context.read<CounterCubit>().increment(),
child: const Text('+'),
),
);
Corrected
Widget build(BuildContext context) => BlocProvider(
create: (_) => CounterCubit(),
child: Builder(
builder: (context) => TextButton(
onPressed: () => context.read<CounterCubit>().increment(),
child: const Text('+'),
),
),
);

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.

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.