Old Scaffold showSnackBar code no longer works in Flutter
'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.
Fix it fast
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 analyze2Match what you see, then jump to its fix
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 analyzeConfirmed by docs.flutter.dev
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 runConfirmed by docs.flutter.dev
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:
- 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
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
Scaffold.of() looks up the widget tree from the context you give it.
3 causes, each with a fixMobile apps
'Floating SnackBar presented off screen' assertion in a Scaffold
Consider constraining the size of these widgets to allow room for the SnackBar to be visible.
A floating SnackBar is placed above the Scaffold's bottom widgets.
3 causes, each with a fixMobile 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 fix