FlutterToast
FlutterToast copied to clipboard
fToast.init(yourNavKey.currentContext!); not working in v8.2.1
Flutter 3.7.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision b06b8b2710 (4 weeks ago) • 2023-01-23 16:55:55 -0800
Engine • revision b24591ed32
Tools • Dart 2.19.0 • DevTools 2.20.1
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
...
return MaterialApp(
navigatorKey: navigatorKey,
and then
FToast fToast = FToast();
...
@override
void initState() {
super.initState();
fToast.init(navigatorKey.currentContext!);
}
With this, while calling fToast.showToast( fails with
VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: Error: Overlay is null.
Please don't use top of the widget tree context (such as Navigator or MaterialApp) or
create overlay manually in MaterialApp builder.
More information
- https://github.com/ponnamkarthik/FlutterToast/issues/393
- https://github.com/ponnamkarthik/FlutterToast/issues/234
What am I doing wrong?
MaterialApp(
builder: FToastBuilder(),
home: MyApp(),
navigatorKey: navigatorKey,
),
have you added builder: FToastBuilder(), in MaterialApp
No I haven't! Thanks a lot.
Ok. This solved the problem I was facing. Thanks. I am very new to Flutter so some of the internals are not known to me yet. On a button click I do Navigator.push and then when I come back to this page I do a Navigator.pop. After this, FToast will crash with the same error as before.
Error: Overlay is null.
I see this in the source code
/// To prevent exception "Looking up a deactivated widget's ancestor is unsafe."
/// which can be thrown if context was unmounted (e.g. screen with given context was popped)
And mounted is false.

I am trying to read up on overlays. Cannot find any direct relation to navigator.pop messing up overlay stacks. Any pointers here would be great.
@aneeskA It says in doc:
@override
void initState() {
super.initState();
fToast = FToast();
// if you want to use context from globally instead of content we need to pass navigatorKey.currentContext!
fToast.init(context);
}
So You can create a global key in your main.dart.
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
and then in initState of your widget
void initState() {
super.initState();
fToast = FToast();
fToast.init(navigatorKey.currentContext!);
}
I hope this helps.