The analyzer warns about using BuildContext after an await
Don'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.
Fix it fast
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 analyze2Match what you see, then jump to its fix
- 1
The 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
The flagged method sits in a class that extends State<...> and uses context directlyThe context is the State's own context propertyFix - 3
You 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
- 1
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/ -r2Fix
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 analyzeConfirmed by dart.dev
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/ -r2Fix
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 analyzeConfirmed by dart.dev
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 analyze2Fix
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix