Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

The analyzer warns about using BuildContext after an await

ErrorDon't use 'BuildContext's across async gaps.

Short answer

The warning is not noise. After an await the widget may be gone, so its BuildContext can point at nothing. Inside a State class, return early with if (!mounted) return. In a function that received the context as an argument, use if (!context.mounted) return. Put the check after every await that is followed by context use.

  • Applies to Dart 3.x, Flutter 3.3x stable
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the analyzer and list every line it flags. Each one has an await above it and a context use below it.

    dart analyze
  2. 2Match what you see, then jump to its fix

    1. 1The flagged method is a plain function, a StatelessWidget handler or a helper with a BuildContext parameterThe context comes from a function argument or a local variableFix
    2. 2The flagged method sits in a class that extends State<...> and uses context directlyThe context is the State's own context propertyFix
    3. 3You added a mounted check and the analyzer still flags a later lineThe check is there but sits before the await, or covers only one awaitFix

What does the error look like?

  • Don't use 'BuildContext's across async gaps.
  • use_build_context_synchronously

Cause 1 of 3

The context comes from a function argument or a local variable

1Check

Look for the await above the flagged line. The widget the context belongs to may have been removed while the future ran.

grep -n -B3 "Navigator.of(context)\|ScaffoldMessenger.of(context)" lib/ -r

2Fix

Test context.mounted after the await and return if it is false.

void onButtonTapped(BuildContext context) async {
  await Future.delayed(const Duration(seconds: 1));
  if (!context.mounted) return;
  Navigator.of(context).pop();
}

3Verify

The warning disappears from that line.

dart analyze

Confirmed by dart.dev

Did this fix it?

Cause 2 of 3

The context is the State's own context property

1Check

A State has a mounted property that tells whether its widget is still in the tree. The linter expects that one here, not context.mounted.

grep -n "extends State<" lib/ -r

2Fix

Check the State's mounted property after the await.

void foo() async {
  await Future.delayed(const Duration(seconds: 1));
  if (!mounted) return;
  Navigator.of(context).pop();
}

3Verify

The warning no longer appears for that method.

dart analyze

Confirmed by dart.dev

Did this fix it?

Cause 3 of 3

The check is there but sits before the await, or covers only one await

1Check

The rule treats every await as a gap. A check made before it says nothing about what happens during it.

dart analyze

2Fix

Move the check below the await, and repeat it after each further await that precedes a context use.

await uploadPhoto();
if (!context.mounted) return;
await saveRecord();
if (!context.mounted) return;
Navigator.of(context).pop();

3Verify

dart analyze is clean for the file.

dart analyze lib/

Confirmed by dart.dev

Did this fix it?

How do I stop it coming back?

  • Write the mounted check in the same edit as the await, so it is never forgotten.
  • Keep the lint on and treat it as an error in CI with dart analyze --fatal-infos.
  • Do the slow work in a controller or notifier and let the widget react to its result.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.