Flutter: Scaffold.of() called with a context that does not contain a Scaffold
You called Scaffold.of(context) (or ScaffoldMessenger) using the same BuildContext that created the Scaffold, which is above it in the tree.
Scaffold.of() called with a context that does not contain a ScaffoldScaffold.of() called with a context that does not contain a Scaffold. No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of()ScaffoldMessenger.of() called with a context that does not contain a ScaffoldMessenger Meaning
of(context) looks upward from the context. The context of the widget that builds the Scaffold is its parent, so it cannot find it. Use a Builder widget to get a context below the Scaffold, or split the body into its own widget. For snack bars use ScaffoldMessenger.of(context) from a context under MaterialApp.
Common causes
- of(context) in the same build method that returns the Scaffold
- Using the State's context from before Scaffold
- Calling from initState or a static function
- Context taken from a different route
- Old Scaffold.of(context).showSnackBar API
⚡ Quick fix
- Wrap the call site in
Builder(builder: (context) => …) - Extract the body into a separate widget
- Use
ScaffoldMessenger.of(context).showSnackBar(...)
Detailed fix by platform
Flutter
Scaffold(body: Builder(builder: (ctx) => TextButton(onPressed: () => ScaffoldMessenger.of(ctx).showSnackBar(const SnackBar(content: Text('Hi'))), child: const Text('Show'))))
How to diagnose
- Context — Above or below Scaffold?
- API — ScaffoldMessenger?
- Route — Same route?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Android license status unknown / licenses not accepted flutter doctor: Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses
- BoxConstraints forces an infinite width Flutter: BoxConstraints forces an infinite width. These invalid constraints were provided to RenderStack's layout() function (TextField in a Row)
- Dependency requires compileSdk 34 or later Android Gradle: Execution failed for task ':app:checkDebugAarMetadata'. Dependency requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs
Most viewed in Flutter / Dart
- setState() called after dispose() Flutter: setState() called after dispose(): _MyState#1a2b3 (lifecycle state: defunct, not mounted)
- A RenderFlex overflowed Flutter: A RenderFlex overflowed by x pixels on the right/bottom
- Null check operator used on a null value Dart/Flutter: Null check operator used on a null value
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 4 Nov 2026