Maaz Zindani

Loading the index…

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

Old Scaffold showSnackBar code no longer works in Flutter

Error'showSnackBar' is deprecated and shouldn't be used.

Short answer

SnackBars are shown by ScaffoldMessenger now. The showSnackBar, hideCurrentSnackBar and removeCurrentSnackBar methods on Scaffold were deprecated in Flutter 1.23 and removed in 3.0. Call the same method names on ScaffoldMessenger.of(context). If you used a Scaffold key, give MaterialApp a scaffoldMessengerKey and call the methods on that.

  • 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 every call that still goes through a Scaffold. The analyzer flags them on old Flutter versions, and on current versions they stop compiling.

    grep -rnE "showSnackBar|hideCurrentSnackBar|removeCurrentSnackBar" lib/
    dart analyze
  2. 2Match what you see, then jump to its fix

    1. 1Scaffold.of(context).showSnackBar(...) in the line the analyzer marksThe code calls Scaffold.of(context) for snack barsFix
    2. 2scaffoldKey.currentState.showSnackBar(...) in a service, a controller or a callbackA GlobalKey<ScaffoldState> is used to show snack bars from outside the widget treeFix

What does the error look like?

  • 'showSnackBar' is deprecated and shouldn't be used.
  • Scaffold.of(context).showSnackBar

Cause 1 of 2

The code calls Scaffold.of(context) for snack bars

1Check

Flutter's breaking-change notes say the three SnackBar methods on Scaffold, showSnackBar, hideCurrentSnackBar and removeCurrentSnackBar, were deprecated in v1.23 and are gone in stable 3.0. The notes also say the Flutter Fix tool does not migrate them, so dart fix will not do this for you.

flutter --version
grep -rn "Scaffold.of(context)" lib/

2Fix

Use the same method names on ScaffoldMessenger. Every MaterialApp already creates one, so no extra widget is needed, and a Builder to reach a context below the Scaffold is no longer necessary for this call.

// before
Scaffold.of(context).showSnackBar(snackBar);

// after
ScaffoldMessenger.of(context).showSnackBar(snackBar);

3Verify

Run the analyzer again and tap the button that shows the snack bar.

dart analyze

Confirmed by docs.flutter.dev

Did this fix it?

Cause 2 of 2

A GlobalKey<ScaffoldState> is used to show snack bars from outside the widget tree

1Check

The migration guide covers this pattern separately. A key on the Scaffold used to give you its state for snack bars, and that key can no longer do the job. The ScaffoldMessenger takes over, and the guide shows a key for it.

grep -rn "GlobalKey<ScaffoldState>" lib/

2Fix

Give MaterialApp a scaffoldMessengerKey, and call the methods on the key's current state. currentState is nullable, so use a null-aware call.

final GlobalKey<ScaffoldMessengerState> messengerKey =
    GlobalKey<ScaffoldMessengerState>();

MaterialApp(
  scaffoldMessengerKey: messengerKey,
  home: const HomePage(),
);

// anywhere in the app
messengerKey.currentState?.showSnackBar(snackBar);

3Verify

Trigger the call from outside a widget, for example after an API call finishes. The snack bar appears.

flutter run

Confirmed by docs.flutter.dev

Did this fix it?

How do I stop it coming back?

  • Treat every deprecation warning as a task for the same sprint, because removed APIs stop compiling after the next big Flutter upgrade.
  • Keep one small helper that shows snack bars, so there is a single place to change.
  • Remember that SnackBars now persist across route changes. Call hideCurrentSnackBar before you leave a screen if the message belongs to that screen only.
  • When you copy a snippet from an old tutorial, run dart analyze before you trust 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:

Related fixes

Next step

Want this handled for good?

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