Maaz Zindani

Loading the index…

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

Flutter throws 'Scaffold.of() called with a context that does not contain a Scaffold'

ErrorScaffold.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.

  • Applies to Flutter 3.35, Dart 3.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1the 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. 2the failing call is Scaffold.of(context).showSnackBar(...)You are showing a SnackBar through the ScaffoldFix
    3. 3the call is in a callback or a helper that has no useful BuildContextYou need the Scaffold state from outside the widget treeFix

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 run

Confirmed by api.flutter.dev

Did this fix it?

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 run

Confirmed by docs.flutter.dev

Did this fix it?

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 run

Confirmed by api.flutter.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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