Flutter throws 'Scaffold.of() called with a context that does not contain a Scaffold'
Scaffold.of() called with a context that does not contain a Scaffold.Short answer
Scaffold.of() looks up the widget tree from the context you give it. If that context belongs to the build method that creates the Scaffold, the Scaffold is below it and the lookup finds nothing. Give the call a context under the Scaffold, or use ScaffoldMessenger for snack bars.
Fix it fast
1Find out which cause you have
Find the Scaffold.of(context) call and check whether that context comes from the same build method that returns the Scaffold.
grep -rn "Scaffold.of(" lib/2Match what you see, then jump to its fix
- 1
the error appears when you tap a button placed directly in the Scaffold's bodyThe context comes from the build method that creates the ScaffoldFix - 2
the failing call is Scaffold.of(context).showSnackBar(...)You are showing a SnackBar through the ScaffoldFix - 3
the call is in a callback or a helper that has no useful BuildContextYou need the Scaffold state from outside the widget treeFix
- 1
What does the error look like?
Scaffold.of() called with a context that does not contain a Scaffold.Scaffold.of() called with a context that does not contain a Scaffold
Cause 1 of 3
The context comes from the build method that creates the Scaffold
1Check
The context argument of a build method is above the widgets it returns, so it cannot find a Scaffold that the same method creates.
grep -rn -B3 "Scaffold.of(context)" lib/2Fix
Wrap the part that needs the Scaffold in a Builder, so it gets a new context that sits under the Scaffold. Splitting that part into its own widget works the same way.
Scaffold(
body: Builder(
builder: (innerContext) => TextButton(
onPressed: () => Scaffold.of(innerContext).openDrawer(),
child: const Text('Menu'),
),
),
drawer: const Drawer(),
)3Verify
Hot restart and tap the button.
flutter runConfirmed by api.flutter.dev
Cause 2 of 3
You are showing a SnackBar through the Scaffold
1Check
The SnackBar API moved from Scaffold to ScaffoldMessenger. MaterialApp provides one by default, so no Builder is needed.
grep -rn "showSnackBar" lib/2Fix
Call ScaffoldMessenger.of(context) instead. It keeps the snack bar across routes and shows it on the current Scaffold.
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Saved')),
);3Verify
The snack bar shows on the current screen.
flutter runConfirmed by docs.flutter.dev
Cause 3 of 3
You need the Scaffold state from outside the widget tree
1Check
The API page names two other options besides a Builder: put a GlobalKey on the Scaffold and read key.currentState, or split the build method into separate widgets.
grep -rn "GlobalKey<ScaffoldState>" lib/2Fix
Attach a GlobalKey to the Scaffold and use its currentState. For snack bars, give MaterialApp a scaffoldMessengerKey instead.
final scaffoldKey = GlobalKey<ScaffoldState>();
Scaffold(key: scaffoldKey, drawer: const Drawer(), body: ...);
scaffoldKey.currentState?.openDrawer();3Verify
The drawer opens from the callback.
flutter runConfirmed by api.flutter.dev
How do I stop it coming back?
- Keep screens small: a widget that uses Scaffold.of should be a child of the Scaffold, not the widget that builds it.
- Show snack bars with ScaffoldMessenger, not with the Scaffold.
- Name the inner context of a Builder differently from the outer one, so you do not pass the wrong one.
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