flutter-context-after-await
Narrow trigger extracted from skills/flutter-conventions §1 (opening rule) and §1.2, so a
BuildContext used after an await routes here directly instead of only through the whole Flutter
block.
When
Any code that does context. — navigation, a snack bar, a dialog, a theme or media-query read, a
provider lookup — reached after an await, a Future.wait, a loop that awaits per item, or an
awaited navigation that returns a result.
Steps
- After any
await, theBuildContextis suspect. It either throws ("this widget has been unmounted") or silently misfires — a snack bar on a scaffold nobody can see, a dialog on a dead navigator — and it's invisible in review because the code reads as straight-line. - In a stateful widget: check
mountedbetween the await and the context use — a check placed before the await is useless, since the widget can unmount during it. - In a stateless widget: capture what you need before the await — the navigator or messenger object itself, not the context.
- In a state holder (cubit/bloc): don't touch context at all. Emit, and let the widget listen.
- One guard per await, not one per method. Every resumption point (each
await, each iteration of an awaiting loop) is its own gap and needs its own check — including after an awaited navigation that returns a result.
Output / checkpoint
Every context. use that follows an await sits behind a fresh mounted check (stateful widget), a
value captured before the await (stateless widget), or is absent entirely (state holder) — with one
guard per resumption point, not one per method.
Guardrails
- Never silence the analyzer's async-context lint with an ignore comment — it flags a latent crash,
not a false positive (
skills/flutter-conventions§1.7). - The full three-shapes-by-location breakdown lives in
skills/flutter-conventions§1; the after-navigation-result variant isskills/flutter-conventions§5.12 — read it before assuming one shape covers every widget kind.
Origin
Extracted from skills/flutter-conventions §1.1-§1.3 (written 2026-09-09). Checked 2026-10-20 against the
Dart linter page for the async-context rule (guard a State's context with the State's mounted, any
other context with its own mounted) and the BuildContext API page (do not store a context, do not cache
values read from it beyond one synchronous function, check mounted after an async gap); both pages' site
terms were not read, facts only, no text copied. Steps 1-4 and the guardrail on the lint match those pages.
Still unsourced, our reasoning: that a check placed before the await is useless and that each resumption point
needs its own guard (step 2 and 5), and the two failure modes in step 1 (throw versus silent misfire), which
no page read states in those terms. 🟡: never run by us. Detail in skills/flutter-conventions/references/origin.md.
Independent verification 2026-10-21: 6 checked, 6 matched, 0 corrected, 0 unverified (fewer than eight checkable points in this block; all were checked).