颤动=> showDialog/AlertDialog =>找不到MaterialLocalizations [英] Flutter => showDialog / AlertDialog => No MaterialLocalizations found

查看:91
本文介绍了颤动=> showDialog/AlertDialog =>找不到MaterialLocalizations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Flutter来说是新手,但印象深刻. 如果想通过Firebase"onMessage"到达PushNotification,我想显示一个对话框.

just new to Flutter but very impressed. I want to show a Dialog if a PushNotification arrives via Firebase "onMessage".

但是每次我得到一个异常找不到MaterialLocalizations".如果试图显示我的对话框. 为了进行测试,我添加了一个RaisedButton来显示此警报,但是同样的问题. 也许有人可以帮助我. 非常感谢!!!

But every Time I get a Exception "No MaterialLocalizations found." if trying to show my Dialog. For testing I added a RaisedButton to show this Alert, but same problem. Perhaps somebody can help me. Thanks a lot!!!

这是小应用程序的全部代码:

Here is my whole code for the small app:

import 'dart:async';

import 'package:firebase_messaging/firebase_messaging.dart';

import 'package:flutter/material.dart';

void main() => runApp(Main());

class Main extends StatefulWidget {
  @override
  _MainState createState() => _MainState();
}

class _MainState extends State<Main> {
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

  Widget _buildDialog(BuildContext context) {
    print("_buildDialog");
    return AlertDialog(
      content: Text("Item  has been updated"),
      actions: <Widget>[
        FlatButton(
          child: const Text('CLOSE'),
          onPressed: () {
            Navigator.pop(context, false);
          },
        ),
        FlatButton(
          child: const Text('SHOW'),
          onPressed: () {
            Navigator.pop(context, true);
          },
        ),
      ],
    );
  }

  void _showPushDialog() {
    print("DIALOG");
    showDialog<bool>(
      context: context,
      builder: (_) => _buildDialog(context),
    ).then((bool shouldNavigate) {
      if (shouldNavigate == true) {
        _navigateToPushDetail();
      }
    });
  }

  void _navigateToPushDetail() {
    print("TODO: Goto...");
  }

  @override
  void initState() {
    super.initState();
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        //_neverSatisfied();
        _showPushDialog();
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
        _navigateToPushDetail();
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
        _navigateToPushDetail();
      },
    );

    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, badge: true, alert: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      print("Push Messaging token: $token");
    });
  }

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

推荐答案

为解决该错误,您需要像下面一样将Main类作为MaterialApp的home参数调用.

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(),
    );
  }
}

&将Main类中的Build方法更新为:

& 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: <Widget>[
        Center(
          child: Text('Hello World'),
        ),
        RaisedButton(
          onPressed: () {
            print("pushed?");
            _showPushDialog(context);
          },
          child: Text("press me"),
        )
      ]),
    );
  }

这篇关于颤动=> showDialog/AlertDialog =&gt;找不到MaterialLocalizations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆