The most common crash in the Flutter apps we're asked to audit is not a null dereference. It is setState() called after dispose(), and it almost always sits behind an await that three reviewers read past. This is our Flutter code audit checklist for async gaps: the specific shapes these bugs take, what catches them mechanically, and what only shows up once real users on real networks hit the app.

Why async gaps survive code review

A reviewer reads a diff top to bottom, and the code looks linear: fetch, then update state, then navigate. The widget lifecycle is not in the diff. Nothing on the page says the user may have popped the route during the await. The bug is a property of time, and code review is a spatial activity.

On a fast office Wi-Fi connection the gap is a few milliseconds and the bug never reproduces. On a congested mobile network, or after a backend retry, the gap is seconds long. That is why these failures cluster in production crash reports and rarely in QA.

The shapes we keep finding

1. The bare setState after await

The textbook form:

Future<void> _load() async {
  final data = await repo.fetch();
  setState(() => _items = data);
}

Flutter 3.7 and later give you if (!mounted) return; and the use_build_context_synchronously lint, but the lint only tracks BuildContext use, not a plain setState. Teams assume the lint covers both. It doesn't.

2. The guard in the wrong place

We regularly see if (mounted) placed before the await instead of after it, or on one branch of a try/catch while the catch block calls setState unguarded. Error paths are the usual offender because they're the least tested.

3. Context captured across the gap

Passing context into a helper that awaits, then uses it, hides the gap from the lint entirely:

Future<void> _submit(BuildContext context) async {
  await api.save();
  Navigator.of(context).pop(); // lint may not follow this across the call boundary
}

A mounted check on the caller's State does nothing here, because the helper may be a free function with no State at all. The fix is to resolve Navigator.of(context) and ScaffoldMessenger.of(context) before the await, or to pass in callbacks instead of a context.

4. Listeners and controllers that outlive the widget

These are not strictly async-gap bugs, but they produce the same crash. Typical forms: a StreamSubscription never cancelled in dispose(), an AnimationController whose .then() fires after disposal, a Timer.periodic that calls setState, or a listener added to a long-lived ChangeNotifier and never removed.

5. Unawaited futures

A fire-and-forget call like _refresh(); inside initState or a button handler has no one to surface its failure and no lifecycle tied to it. Turn on unawaited_futures; when the call really is intentional, wrap it in unawaited() so the next reader knows it was a decision.

Grep patterns and lint rules that catch them

Start with the tooling, because it is cheap. In analysis_options.yaml enable use_build_context_synchronously, unawaited_futures, cancel_subscriptions, and close_sinks. Treat the first as an error, not a warning; warnings in inherited codebases are wallpaper.

Then grep for what the lints miss:

  • rg -n "setState\(" -B6 lib | rg "await" to list every setState with an await in the preceding lines, then check each for a mounted guard by hand.
  • rg -n "Navigator\.of\(context\)|ScaffoldMessenger\.of\(context\)|showDialog" lib and read what precedes each hit. Anything after an await without a guard is a candidate.
  • rg -n "Timer\.periodic|\.listen\(" lib and confirm a matching cancel() in dispose for each.
  • rg -n "addListener\(" lib against removeListener. The counts should match; in inherited apps they usually don't.
  • rg -n "context\." lib -g "*.dart" inside any non-widget class: repositories and view-models holding a BuildContext are an architecture problem, not just a lifecycle one.

This pass takes an afternoon on a mid-sized app and typically produces a list long enough to be uncomfortable.

What only fails in production

Some cases evade both grep and lint, and we only confirm them by reproducing them:

  • Guard present, logic still wrong. if (!mounted) return; prevents the crash but can skip cleanup that must run regardless, such as releasing a lock, resetting an upload flag, or recording that a payment call completed. The widget is gone; the side effect still happened. State that matters belongs in a layer that outlives the widget.
  • Out-of-order responses. A search field fires two requests; the slower, older one lands last and overwrites newer results. No dispose is involved, so no crash, only wrong data. A request token or cancellation of the previous call fixes it.
  • Route transitions. A widget can be mounted but no longer the visible route, so a dialog or snackbar appears over the wrong screen. mounted says nothing about that.
  • Hot-path rebuild timing. State changes arriving mid-frame on low-end Android devices expose ordering assumptions that emulators never will. Test on slow hardware with throttled networking, not only on a flagship phone.

Fixing it structurally, not line by line

Sprinkling mounted checks works, but it treats the symptom. When we audit a codebase, we push for the async work to leave the widget: put the loading logic in a controller or state-management layer that owns cancellation, and let the widget only render state. Then a disposed screen means a cancelled subscription, not a callback that wakes up and touches a dead tree. A short test that pumps a widget, starts a slow fake repository call, pops the route, and completes the future will catch most regressions in CI.

If you only have time for one thing, run the lint at error severity and review every grep hit above that involves an await. That covers the bulk of what we find.

orithLabs does this kind of work for teams who've inherited a Flutter app and don't trust it yet: a code audit of the async and lifecycle paths, a written list of what's risky and what's fine, and, if you want, direct help fixing it. If you're staring at a crash report that says "setState() called after dispose()," we're happy to talk through it with you, no pitch attached.