toastification icon indicating copy to clipboard operation
toastification copied to clipboard

Global Navigator Key context is not working with toastification plugin

Open ilyasarafathemvigo opened this issue 1 year ago • 1 comments

Global navigator key context is not working toastification.show( context: globalNavigatorKey.currentContext!, title: const Text('Hello, world!'), autoCloseDuration: const Duration(seconds: 5), );

To Reproduce Steps to reproduce the behavior:

  1. Add a global navigator key in the main.dart file Screenshot 2024-08-26 at 3 20 38 PM

  2. Pass the navigator key to MaterialApp Screenshot 2024-08-26 at 3 21 47 PM

  3. Call the toast using this globalKey context Screenshot 2024-08-26 at 3 22 56 PM

  4. Check the console log ` [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'package:toastification/src/core/toastification_overlay_state.dart': Failed assertion: line 18 pos 5: '_debugInitialized': Toastification is not initialized! E/flutter ( 3507): ensure your app is wrapped with a ToastificationWrapper widget E/flutter ( 3507): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61) E/flutter ( 3507): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5) E/flutter ( 3507): #2 findToastificationOverlayState (package:toastification/src/core/toastification_overlay_state.dart:18:5) E/flutter ( 3507): #3 Toastification.showCustom (package:toastification/src/core/toastification.dart:136:36) E/flutter ( 3507): #4 Toastification.show (package:toastification/src/core/toastification.dart:264:12) E/flutter ( 3507): #5 _TestScreenState.build. (package:wsac/presentation/test_screen.dart:61:32) E/flutter ( 3507): #6 _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1154:21) E/flutter ( 3507): #7 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:275:24) E/flutter ( 3507): #8 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:654:11) E/flutter ( 3507): #9 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:311:5) E/flutter ( 3507): #10 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:244:7) E/flutter ( 3507): #11 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:630:9) E/flutter ( 3507): #12 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:98:12) E/flutter ( 3507): #13 PointerRouter._dispatchEventToRoutes. (package:flutter/src/gestures/pointer_router.dart:143:9) E/flutter ( 3507): #14 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:625:13) E/flutter ( 3507): #15 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:141:18) E/flutter ( 3507): #16 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:127:7) E/flutter ( 3507): #17 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:488:19) E/flutter ( 3507): #18 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:468:22) E/flutter ( 3507): #19 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:333:11) E/flutter ( 3507): #20 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:413:7) E/flutter ( 3507): #21 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:376:5) E/flutter ( 3507): #22 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:323:7) E/flutter ( 3507): #23 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:292:9) E/flutter ( 3507): #24 _invoke1 (dart:ui/hooks.dart:186:13) E/flutter ( 3507): #25 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:424:7) E/flutter ( 3507): #26 _dispatchPointerDataPacket (dart:ui/hooks.dart:119:31) E/flutter ( 3507):

`

Expected behavior Should display a toast without any error

Flutter information:

  • OS: android and iOS
  • Flutter Version: 3.24.0
  • Toastification Version 2.0.0

NB: All the widgets are working fine with global navigator key

ilyasarafathemvigo avatar Aug 26 '24 09:08 ilyasarafathemvigo

Same issue. I've been using injectable for my AppRouter and getting context like this final context = getIt<AppRouter>().navigatorKey.currentContext!;

context is not null, and the error is the same as mentioned above

PS: for me issue happens on Web

komakur avatar Sep 10 '24 08:09 komakur

@komakur @ilyasarafathemvigo Thank you both for the context you provided on this issue—it was really helpful! It allowed me to identify and fix the problem.

The issue is that you cannot use the context of the global key. Instead, you can pass the OverlayState from the navigator key like this:

final GlobalKey<NavigatorState> globalNavigatorKey = GlobalKey<NavigatorState>();

toastification.show(
  overlayState: navigatorKey.currentState?.overlay,
  autoCloseDuration: const Duration(seconds: 5),
  title: Text('Hello, World!'),
);

Let me know if you have any questions or need further clarification!

payam-zahedi avatar Dec 28 '24 11:12 payam-zahedi

I also added an section in our Document for that you can check it from here:

https://github.com/payam-zahedi/toastification?tab=readme-ov-file#using-globalnavigatorkey

payam-zahedi avatar Dec 28 '24 11:12 payam-zahedi