No MaterialLocalizations found (Flutter)

Posted z45281625

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了No MaterialLocalizations found (Flutter)相关的知识,希望对你有一定的参考价值。

在显示SimpleDialog时候程序报错 No MaterialLocalizations found 没有找到 MaterialLocalizations

 

搜索找到原因 runApp 需要先调用 StatelessWidget 返回一个 MaterialApp实例.

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

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: Welcome to Flutter,
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

 也就是说runApp 直接接受一个 StatefulWidget就会出现这个错误

 

完整代码

import package:flutter/material.dart;

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

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: Welcome to Flutter,
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePage createState() {
    return _HomePage(title: 简单对话框组件);
  }
}

enum Department { treasury, state }

class _HomePage extends State<HomePage> {
  _HomePage({Key key, @required this.title});

  final String title;

  ThemeData themeData = new ThemeData.light();
  Future<void> _askedToLead() async {
    switch (await showDialog<Department>(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            title: const Text(Select assignment),
            children: <Widget>[
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, Department.treasury);
                },
                child: const Text(Treasury department),
              ),
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, Department.state);
                },
                child: const Text(State department),
              ),
            ],
          );
        })) {
      case Department.treasury:
        // Let‘s go.
        // ...
        break;
      case Department.state:
        // ...
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: this.title,
      debugShowCheckedModeBanner: false,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(this.title),
        ),
        body: null,
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.format_list_numbered),
          onPressed: _askedToLead,
        ),
      ),
      theme: themeData,
    );
  }
}

 

以上是关于No MaterialLocalizations found (Flutter)的主要内容,如果未能解决你的问题,请参考以下文章

有一首英文歌曲不知道名字 只知道一句歌词 no no no no

抛弃与修复原理在经济管理中的应用吗

如何使 --no-ri --no-rdoc 成为 gem 安装的默认值? [复制]

如何使用 spark 过滤 Hive 中的记录

Jetpack Compose | Compose 滑动列表真的需要使用LazyColumn吗?No No No!

Jetpack Compose | Compose 滑动列表真的需要使用LazyColumn吗?No No No!