Flutter => showDialog / AlertDialog => No MaterialLocalizations found

前端 未结 3 970
情歌与酒
情歌与酒 2021-02-20 11:47

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

3条回答
  •  梦毁少年i
    2021-02-20 12:23

    In Order to Fix the error, You need to Call Your Main class as a home parameter of MaterialAppas Like Below.

    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Welcome to Flutter',
          debugShowCheckedModeBanner: false,
          home: Main(),
        );
      }
    }
    

    & update your Build Method in Main Class as:

    @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Welcome to Flutter'),
          ),
          body: Column(children: [
            Center(
              child: Text('Hello World'),
            ),
            RaisedButton(
              onPressed: () {
                print("pushed?");
                _showPushDialog(context);
              },
              child: Text("press me"),
            )
          ]),
        );
      }
    

提交回复
热议问题