You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[material_ui] MaterialBanner cannot be dismissed once creation context is deactivated #89705
Looking up a deactivated widget's ancestor is unsafe.At this point the state of the widget's element tree is no longer stable.To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
changed the title [-]When the page returns, showMaterialBanner does not disappear, hideCurrentMaterialBanner reports an error[/-][+]MaterialBanner cannot be dismissed once creation context is deactivated[/+]on Sep 29, 2021
Issue is reproducible on master and stable with code sample provided by author.
If a MaterialBanner is created on PageB and PageB is popped from the stack, we are stuck with a MaterialBanner that cannot be dismissed, because it throws the error in the logs below.
This error is to be expected because the Scaffold that originally created the MaterialBanner is no longer available, however, that then introduces the problem stated above.
A possible workaround is to use the scaffoldMessengerKey associated with the MaterialApp to be showing and dismissing the MaterialBanner.
Due to the nature of the MaterialBanner, I believe it would be best if this feature came out of the box without using the scaffoldMessengerKey.
Some possible solutions might be:
dismiss the banner if the creation context is closed (but that goes against the nature of the Banner)
Attach it to the root context so that it can be deactivated at any time no matter where it is created, although I'm not certain what side effects this will have, if any. (This solution seems similar to using the scaffoldMessengerKey associated with the MaterialApp).
I am going to label it so that more insight can be provided.
@jiabin87428 You can try the sample below in the mean time to see if it suits your needs
Restarted application in 397ms.════════ Exception caught by gesture ═══════════════════════════════════════════The following assertion was thrown while handling a gesture:Looking up a deactivated widget's ancestor is unsafe.At this point the state of the widget's element tree is no longer stable.To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.When the exception was thrown, this was the stack#0 Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure>#1 Element._debugCheckStateIsActiveForAncestorLookup#2 Element.dependOnInheritedWidgetOfExactType#3 ScaffoldMessenger.maybeOf#4 SecondPage.build.<anonymous closure>.<anonymous closure>#5 _InkResponseState._handleTap#6 GestureRecognizer.invokeCallback#7 TapGestureRecognizer.handleTapUp#8 BaseTapGestureRecognizer._checkUp#9 BaseTapGestureRecognizer.handlePrimaryPointer#10 PrimaryPointerGestureRecognizer.handleEvent#11 PointerRouter._dispatch#12 PointerRouter._dispatchEventToRoutes.<anonymous closure>#13 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:525:8)#14 PointerRouter._dispatchEventToRoutes#15 PointerRouter.route#16 GestureBinding.handleEvent#17 GestureBinding.dispatchEvent#18 RendererBinding.dispatchEvent#19 GestureBinding._handlePointerEventImmediately#20 GestureBinding.handlePointerEvent#21 GestureBinding._flushPointerEventQueue#22 GestureBinding._handlePointerDataPacket#26 _invoke1 (dart:ui/hooks.dart:169:10)#27 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:293:7)#28 _dispatchPointerDataPacket (dart:ui/hooks.dart:88:31)(elided 3 frames from dart:async)Handler: "onTap"Recognizer: TapGestureRecognizer#50180 debugOwner: GestureDetector state: possible won arena finalPosition: Offset(328.3, 134.3) finalLocalPosition: Offset(15.3, 21.3) button: 1 sent tap down════════════════════════════════════════════════════════════════════════════════
flutter doctor -v
[✓] Flutter (Channel stable, 2.5.1, on macOS 11.5.1 20G80 darwin-arm, locale en-GB) • Flutter version 2.5.1 at /Users/nexus/dev/sdks/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision ffb2ecea52 (12 days ago), 2021-09-17 15:26:33 -0400 • Engine revision b3af521a05 • Dart version 2.14.2[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-31, build-tools 31.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) • All Android licenses accepted.[✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.5.1, Build version 12E507 • CocoaPods version 1.11.2[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome[✓] Android Studio (version 2020.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)[✓] VS Code (version 1.60.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.26.0[✓] Connected device (3 available) • iPhone 12 Pro (mobile) • 3685B616-79C3-49F2-9E32-06E6B9A7C48E • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 11.5.1 20G80 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 94.0.4606.61• No issues found!
[✓] Flutter (Channel master, 2.6.0-12.0.pre.135, on macOS 11.5.1 20G80 darwin-arm, locale en-GB) • Flutter version 2.6.0-12.0.pre.135 at /Users/nexus/dev/sdks/flutters • Upstream repository https://github.com/flutter/flutter.git • Framework revision 3074c9e04b (6 hours ago), 2021-09-28 22:08:04 -0700 • Engine revision bccb3a57eb • Dart version 2.15.0 (build 2.15.0-156.0.dev)[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-31, build-tools 31.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) • All Android licenses accepted.[✓] Xcode - develop for iOS and macOS (Xcode 12.5.1) • Xcode at /Applications/Xcode.app/Contents/Developer • CocoaPods version 1.11.2[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome[✓] Android Studio (version 2020.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)[✓] VS Code (version 1.60.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.26.0[✓] Connected device (3 available) • iPhone 12 Pro (mobile) • 3685B616-79C3-49F2-9E32-06E6B9A7C48E • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 11.5.1 20G80 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 94.0.4606.61• No issues found!
Reacted by Alexander Gutheil, Mohammad Qavam, Enrique García Cruz, Abhi, Miguel Jara and Fabián Bardecio
In case someone is looking for a working example. Here's some code you can follow:
1- Create a global scaffold key that has the MaterialApp's context (Remember, MaterialApp is the first widget in our tree, so it has a global context).
For that purpose, I've created a class that will manage our global keys and that also has null support.
import 'package:flutter/material.dart';
class GlobalKeys {
static final GlobalKey<ScaffoldMessengerState> globalScaffoldKey =
GlobalKey<ScaffoldMessengerState>();
static ScaffoldMessengerState get globalScaffold {
final context = globalScaffoldKey.currentState;
if (context == null) {
throw Exception(
'ScaffoldMessengerContext not found. You must initialize it in the MaterialApp widget before using it',
);
}
return context;
}
}
3- Finally, use the global scaffold messenger to handle your materialBanner.
Note
This function can be anywhere in your code, the only important thing here is how we use the globalScaffoldMessenger.
void showMaterialBanner() {
final globalScaffoldMessanger = GlobalKeys.globalScaffold;
//* How to hide materialBanners.
globalScaffoldMessanger.hideCurrentMaterialBanner();
//* How to show materialBanners.
globalScaffoldMessanger.showMaterialBanner(
MaterialBanner(
content: Text('Hello World!')
actions: [
//* Show a close button that works anywhere in your app, regardless of the current context.
IconButton.filledTonal(
icon: const Icon(Icons.close),
onPressed: () =>
globalScaffoldMessanger.hideCurrentMaterialBanner(),
),
],
),
);
}
changed the title [-]MaterialBanner cannot be dismissed once creation context is deactivated[/-][+][material_ui] MaterialBanner cannot be dismissed once creation context is deactivated[/+]on Sep 29, 2026
Activity
danagbemava-nc commentedon Sep 9, 2021
Hi @jiabin87428, thanks for filing the issue.
Can you please provide your
flutter doctor -vandflutter run --verboselogs, and a minimal complete reproducible code sample?Thank you
jiabin87428 commentedon Sep 29, 2021
flutter doctor -v
jiabin87428 commentedon Sep 29, 2021
sample code
[-]When the page returns, showMaterialBanner does not disappear, hideCurrentMaterialBanner reports an error[/-][+]MaterialBanner cannot be dismissed once creation context is deactivated[/+]danagbemava-nc commentedon Sep 29, 2021
Issue is reproducible on
masterandstablewith code sample provided by author.If a
MaterialBanneris created onPageBandPageBis popped from the stack, we are stuck with aMaterialBannerthat cannot be dismissed, because it throws the error in the logs below.This error is to be expected because the Scaffold that originally created the
MaterialBanneris no longer available, however, that then introduces the problem stated above.A possible workaround is to use the
scaffoldMessengerKeyassociated with theMaterialAppto be showing and dismissing theMaterialBanner.Due to the nature of the
MaterialBanner, I believe it would be best if this feature came out of the box without using thescaffoldMessengerKey.Some possible solutions might be:
rootcontext so that it can be deactivated at any time no matter where it is created, although I'm not certain what side effects this will have, if any. (This solution seems similar to using thescaffoldMessengerKeyassociated with theMaterialApp).I am going to label it so that more insight can be provided.
@jiabin87428 You can try the sample below in the mean time to see if it suits your needs
sample with scaffoldMessengerKey
error logs
flutter doctor -v
1 remaining item
Miguel-A-Jara commentedon Dec 30, 2023
In case someone is looking for a working example. Here's some code you can follow:
1- Create a global scaffold key that has the MaterialApp's context (Remember, MaterialApp is the first widget in our tree, so it has a global context).
2- Initialize
globalScaffoldKeyinmain.dart.3- Finally, use the global scaffold messenger to handle your
materialBanner.Note
This function can be anywhere in your code, the only important thing here is how we use the globalScaffoldMessenger.
Aturex1 commentedon Mar 7, 2024
Maybe you should check this answer on StackOverflow out.
Just wrap the MaterialBanner action with a Builder and everything works (at least for me)
flutter-triage-bot commentedon Sep 23, 2026
This issue is missing a priority label. Please set a priority label when adding the
triaged-designlabel.[-]MaterialBanner cannot be dismissed once creation context is deactivated[/-][+][material_ui] MaterialBanner cannot be dismissed once creation context is deactivated[/+][material_ui] Document and demonstrate safe MaterialBanner dismissal …