错误信息
E/flutter ( 7426): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold.
E/flutter ( 7426): No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of(). This usually happens when the context provided is from the same StatefulWidget as that whose build function actually creates the Scaffold widget being sought.
E/flutter ( 7426): There are several ways to avoid this problem. The simplest is to use a Builder to get a context that is “under” the Scaffold. For an example of this, please see the documentation for Scaffold.of():
E/flutter ( 7426): https://docs.flutter.io/flutter/material/Scaffold/of.html
E/flutter ( 7426): A more efficient solution is to split your build function into several widgets. This introduces a new context from which you can obtain the Scaffold. In this solution, you would have an outer widget that creates the Scaffold populated by instances of your new inner widgets, and then in these inner widgets you would use Scaffold.of().
E/flutter ( 7426): A less elegant but more expedient solution is assign a GlobalKey to the Scaffold, then use the key.currentState property to obtain the ScaffoldState rather than using the Scaffold.of() function.
E/flutter ( 7426): The context used was:
E/flutter ( 7426): Home(state: _HomeState#497fc)
E/flutter ( 7426): #0 Scaffold.of (package:flutter/src/material/scaffold.dart:1156:5)
E/flutter ( 7426): #1 _HomeState.build.. (package:my_app/home_widget.dart:46:28)
E/flutter ( 7426): #2 State.setState (package:flutter/src/widgets/framework.dart:1122:30)
E/flutter ( 7426): #3 _HomeState.build. (package:my_app/home_widget.dart:45:17)
E/flutter ( 7426): #4 _PopupMenuButtonState.showButtonMenu. (package:flutter/src/material/popup_menu.dart)
E/flutter ( 7426): #5 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter ( 7426): #6 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 7426): #7 _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
E/flutter ( 7426): #8 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
E/flutter ( 7426): #9 Future._propagateToListeners (dart:async/future_impl.dart:668:32)
E/flutter ( 7426): #10 Future._completeWithValue (dart:async/future_impl.dart:483:5)
E/flutter ( 7426): #11 Future._asyncComplete. (dart:async/future_impl.dart:513:7)
E/flutter ( 7426): #12 _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 7426): #13 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 7426): #14 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter ( 7426): #15 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter ( 7426): #16 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 7426): #17 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

解决方案

使用Builder来包装

@override
Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Demo')),body: Builder(// Create an inner BuildContext so that the onPressed methods// can refer to the Scaffold with Scaffold.of().builder: (BuildContext context) {return Center(child: RaisedButton(child: Text('SHOW A SNACKBAR'),onPressed: () {Scaffold.of(context).showSnackBar(SnackBar(content: Text('Hello!'),));},),);},),);
}

Flutter使用Scaffold报错。相关推荐

  1. `flutter build aar 报错:Because xxx requires SDK version >=2.16.1<3.0.0,version solving failed pub get

    项目场景: flutter build aar 报错:Because xxx requires SDK version >=2.16.1<3.0.0,version solving fai ...

  2. flutter upgrade升级报错

    升级flutter SDK: 报错如下 D:\diskWork\androidStudiop\yzz_flutter_app>flutter upgrade ProcessException: ...

  3. 【错误记录】发布 Flutter 插件包报错 ( ‘gmail.com‘ has insufficient permissions to upload new versions of package)

    文章目录 一.报错信息 二.解决方案 一.报错信息 推送 Flutter 包到中央仓库 , 第一次推送报错 , D:\002_Project\002_Android_Learn\flutter_pac ...

  4. 【错误记录】发布 Flutter 插件包报错 ( Failed to upload the package.pub finished with exit code 1 )

    文章目录 一.报错信息 二.解决方案 一.报错信息 发布插件包 , 确认发布 , 校验了 Google 账号 , 之后报错如下 : Failed to upload the package. pub ...

  5. 【错误记录】发布 Flutter 插件包报错 ( It‘s strongly recommended to include a “homepage“ or “repository“ field )

    文章目录 一.报错信息 二.解决方案 一.报错信息 发布 Flutter 插件包到中央仓库前 , 先执行 flutter packages pub publish --dry-run 命令 , 检查插 ...

  6. flutter ios打包_关于Flutter iOS打包报错的问题

    今天升级Xcode之后,使用打包机打iOS包的时候,报了一个错,报错的信息如下: note: Using new build systemnote: Building targets in paral ...

  7. Flutter踩坑之flutter doctor --android-licenses报错

    首先命令行键入flutter doctor命令检查你的环境,你可能会遇到以下问题: 执行完以上命令命令行会提示你执行flutter doctor --android-licenses命令来解决此问题. ...

  8. Flutter开发:运行flutter upgrade命令报错Exception:Flutter failed to create a directory at…解决方法

    前言 在Flutter开发中,经常会遇到因为更新而引起的报错,而且最近两年Flutter不断完善和更新的速度越来越快,这就需要Flutter相关的开发者时刻保持不断学习的心态,来应对在Flutter开 ...

  9. Flutter开发:报错The following assertion was thrown resolving an image codec:Unable to…的解决方法

    前言 在Flutter开发中,经常会遇到一些稀奇古怪的的报错,比如版本更新之后会报错.文件没有导入会报错.第三方插件版本不一致的报错等等,而且最近几年Flutter不断完善和更新的速度越来越快,这就需 ...

最新文章

  1. PostgreSQL_row_number() over()
  2. ALEIDoc EDI(9)--others
  3. 科技管理的作业选题 很重要
  4. iphone数据存储之-- Core Data的使用(一)
  5. setjump和longjump
  6. 5.14 js对象 函数 js操作document对象
  7. 阿里AI界的新伙伴,1秒钟自动生成20000条文案
  8. 十年维护专家的“秘籍
  9. 「leetcode」513.找树左下角的值(详解)
  10. jetty jndi mysql_jetty配置jndi数据源
  11. MATLAB plot画线的颜色设定
  12. 仿美团外卖小程序源码
  13. 傅里叶分析——傅里叶级数
  14. mysql统计 同比 环比
  15. Android烧录镜像文件介绍
  16. 1946年产生的第一台电子计算机6,1946年第一台计算机在美国诞生——青夏教育精英家教网——...
  17. Android市场-开发者账号注册等-移动开放平台网址收藏
  18. 从程序员的角度来看为什么我们需要工作流
  19. Exploratory Social Network Analysis with Pajek(第三版)11
  20. 基于Java(SSH 框架)+MySQL 实现的物流配送管理系统【100010488】

热门文章

  1. linux+gpfs配置文件,centos7 安装gpfs 4.1.1.3
  2. 最新自适应安防监控类网站源码+易优CMS内核
  3. windows sdk 窗体和消息
  4. $ adb shell am start -n “com.***“ -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
  5. 18位身份证号码最后一位校验码的计算方法
  6. Java房地产销售管理系统_JAVA JSP房地产销售系统 源代码 论文
  7. [含LW+辩论PPT+任务书+中期检查表+源码等]javaweb企业员工信息管理系统的设计与实现薪酬|请假|薪资|工资[包运行成功]计算机毕业设计Java项目
  8. python素数生成器_在python中用滤波器和生成器生成无穷素数
  9. Plant simulation16.0 汽车总装车间仿真系列第一步:整体流程设定
  10. mount命令使用详解