just new to Flutter but very impressed. I want to show a Dialog if a PushNotification arrives via Firebase \"onMessage\".
But every Time I get a Exception \"No MaterialL
This solution works on both StatelessWidget
widget and StatefulWidget
widget.
On the top, in your declaration you can create a static navKey
:
class MyApp extends StatefulWidget {
final String title; // sample var you want to pass to your widget
static final navKey = new GlobalKey();
const MyApp({Key navKey, this.title}) : super(key: navKey);
@override
State createState() => _MyAppState();
}
On the layout part you should use the key:
return MaterialApp(
navigatorKey:MyApp.navKey,
title: widget.title,
...
So, when you need the current context for your dialog or other widget, in the state part you can do :
@override
void initState() {
final context = MyApp.navKey.currentState.overlay.context;
showMyCustomDialog(context);
...
super.initState();
}