payam-zahedi / toastification

Toastification is a Flutter package for displaying customizable toast messages. It provides predefined widgets for success, error, warning, and info messages, as well as a custom widget for flexibility. With Toastification, you can add and manage multiple toast messages at the same time with ease.
https://payamzahedi.com/toastification/
BSD 3-Clause "New" or "Revised" License
596 stars 47 forks source link

Global Navigator Key context is not working with toastification plugin #129

Open ilyasarafathemvigo opened 3 months ago

ilyasarafathemvigo commented 3 months ago

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:

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

komakur commented 2 months ago

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