Flutter analyzer says the name 'context' is not defined
Undefined name 'context'. Try correcting the name to one that is defined, or defining the name.Short answer
context is not a global. It exists only as a parameter of build() and similar callbacks, or as the context property inside a State class. Code in a plain class, a top-level function, a static method or a field initializer has no such name. Pass a BuildContext in as an argument, or move the code into a State method.
Fix it fast
1Find out which cause you have
Look at where the failing line sits: inside build(), inside a State method, or somewhere else. That decides which cause below applies.
flutter analyze2Match what you see, then jump to its fix
- 1
The failing line is in a service, helper or controller class that is not a widget or a StateThe code is in a plain class, a top-level function or a static methodFix - 2
The line is a field declaration or in the constructor of a State subclass, not in a methodThe code runs in a field initializer or constructor of a State classFix - 3
Other names from the same package are also undefined, or the variable is called ctx or buildContextThe name is spelled differently or comes from a file you did not importFix
- 1
What does the error look like?
Undefined name 'context'. Try correcting the name to one that is defined, or defining the name.
Cause 1 of 3
The code is in a plain class, a top-level function or a static method
1Check
A name is only defined where a declaration is in scope. In a class that does not extend State, no context exists, and a StatelessWidget only gets one as a parameter of build().
class Auth {
void goHome() {
Navigator.of(context).pop(); // 'context' is not declared here
}
}2Fix
Declare the name: take a BuildContext as a parameter and let the widget pass its own.
class Auth {
void goHome(BuildContext context) {
Navigator.of(context).pop();
}
}
// in a widget
onPressed: () => Auth().goHome(context),3Verify
The analyzer no longer reports the name.
flutter analyzeConfirmed by dart.dev
Cause 2 of 3
The code runs in a field initializer or constructor of a State class
1Check
A State object is linked to its context by the framework before initState is called. Anything that runs earlier, such as a field initializer, cannot rely on it.
class _PageState extends State<Page> {
final size = MediaQuery.of(context).size; // too early
}2Fix
Move the code into a method that runs after the link: initState, or build.
class _PageState extends State<Page> {
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return Text('$size');
}
}3Verify
Analyze, then run the screen.
flutter analyzeConfirmed by api.flutter.dev
Cause 3 of 3
The name is spelled differently or comes from a file you did not import
1Check
Compare the name at the failing line with the parameter in the enclosing function signature.
grep -n "BuildContext" lib/path/to/file.dart2Fix
Correct the spelling to the declared parameter name, or add the import that declares the symbol.
import 'package:flutter/widgets.dart';3Verify
Run the analyzer again.
flutter analyzeConfirmed by dart.dev
How do I stop it coming back?
- Pass BuildContext into helpers explicitly instead of storing it in fields.
- Keep navigation and dialog calls in widgets; let services return a result and let the widget act on it.
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