美文网首页初见
Flutter开发 踩坑记录

Flutter开发 踩坑记录

作者: Smalla | 来源:发表于2020-04-26 17:16 被阅读0次
前言
  • 最近有时间在研究Flutter开发,从搭建框架(可以参考文章:Flutter基本配置搭建)到开始着手开发Demo项目,体验到Flutter开发的快捷、高效。现将Flutter开发中遇到的问题逐步整理出来,供大家参考:
踩坑记录

1、先看错误日志:

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following assertion was thrown building MyApp(state: _BottomNavigationBarState#ccb0e):
flutter: MediaQuery.of() called with a context that does not contain a MediaQuery.
flutter: No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().
flutter: This can happen because you do not have a WidgetsApp or MaterialApp widget (those widgets introduce
flutter: a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.
flutter: The context used was:
flutter:   Scaffold
flutter:
flutter: The relevant error-causing widget was:
flutter:   MyApp 
lib/…/Base/main.dart:7
flutter:
flutter: When the exception was thrown, this was the stack:

问题代码 :

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _bottomNavPages[_selectedIndex],
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              title: Text("tab1"),
              icon:Image.asset("normal.png")),
          BottomNavigationBarItem(
              title: Text("tab2"),
              icon: Image.asset("normal.png")),
          BottomNavigationBarItem(
              title: Text("tab3"),
              icon: Image.asset("normal.png")),
          BottomNavigationBarItem(
              title: Text("tab4"),
              icon:Image.asset("normal.png")),
        ],
        backgroundColor: Colors.white,
        currentIndex: _selectedIndex,
        unselectedItemColor: Colors.grey,
        selectedItemColor: Colors.orange
      ),
    );
  }

提炼日志中的一句关键信息:
This can happen because you do not have a WidgetsApp or MaterialApp widget XXX
这里是布局底部四个Tabbar,是一个Material组件,所以用Scaffold包裹,这里就会报错,需要用MaterialApp再包裹一层,解决如下:

@override
  Widget build(BuildContext context) {
    return MaterialApp(
       home: Scaffold(
       body: _bottomNavPages[_selectedIndex],
       bottomNavigationBar: BottomNavigationBar(
       items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              title: Text("tab1"),
              icon:Image.asset("normal.png")),
          BottomNavigationBarItem(
              title: Text("tab2"),
              icon: Image.asset("normal.png")),
          BottomNavigationBarItem(
              title: Text("tab3"),
              icon: Image.asset("normal.png")),
          BottomNavigationBarItem(
              title: Text("tab4"),
              icon:Image.asset("normal.png")),
        ],
        backgroundColor: Colors.white,
        currentIndex: _selectedIndex,
        unselectedItemColor: Colors.grey,
        selectedItemColor: Colors.orange
       ),
      ),
    );
  }

2、

相关文章

  • Flutter 开发记录

    Flutter 开发踩坑记录(干货总结)

  • channel ios framework to Flutter

    今天开发Flutter插件,踩了些坑,记录一下1.Android Studio 新建Flutter 项目,选择pl...

  • Flutter开发 踩坑记录

    前言 最近有时间在研究Flutter开发,从搭建框架(可以参考文章:Flutter基本配置搭建)到开始着手开发De...

  • flutter catcher踩坑

    最近自己在写一个独立APP, 用flutter开发, 不定期发布一些踩坑记录, 这篇文章是用来结合catcher,...

  • flutter 踩坑记录

    参考资料 技术胖在 bilibili 的 flutter 系列视频 坑 1. Android Studio 无法...

  • Flutter踩坑记录

    安卓打包失败 如果遇到failed to respond,如果gradle里配置的是google()和maven(...

  • Flutter踩坑记录

    1 去除Debug 标签 问题:默认创建的Flutter应用运行时,屏幕右上角会带Debug标签解决:在MyApp...

  • Flutter踩坑记录

    如果页面整个空白,说明页内有错误,可以分别注释排查 Textfield不能直接放在Row中,因为不确定Textfi...

  • Flutter踩坑记录

    1.Flutter的UI开发【1+1+0.5=2.5】轮播图和自定义指示器【耗时1天】布局越界问题,布局的宽度高度...

  • Flutter 踩坑记录

    1.问题描述: 一个聊天对话页面,由于对话框形状需要自定义,因此采用了CustomPainter来自定义绘制对话框...

网友评论

    本文标题:Flutter开发 踩坑记录

    本文链接:https://www.haomeiwen.com/subject/vqdjwhtx.html