错误信息
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. [转]iOS 远程推送(APNs)
  2. AAAI 2020 | MaskGEC:通过动态掩蔽改善语法纠错
  3. mybatis实体类注解_SpringBoot2.x系列教程43--整合使用Mybatis
  4. 第二章:2.2 开始第一个 web Django 项目的创建和应用
  5. RUNOOB python练习题 39 数组排序
  6. 前端学习(2311):react中处理跨域问题
  7. UpdatePanel 的 UpdateMode 和 ChildrenAsTriggers(较好的总结了前面几篇博客的内容)
  8. Python元组,列表,解构和循环
  9. 福昕pdf虚拟打印机_福昕扫描王将取代扫描仪,OCR文字识别应用办公标配
  10. 全面接触PDF:最好用的PDF软件汇总
  11. Android Studio 学生课程签到系统
  12. Arduino -uno 核心板 之中级系列3 QAU04生日快乐歌实验
  13. Aspect基础使用方法
  14. 生活妙语--智慧语言
  15. 2021-12-22 AndroidR 电池信息 简单分析记录
  16. NOIP普及组历届真题(1997~2018)
  17. Python_变量声明
  18. 前端使用jquery.wordexport.js导出word
  19. Rasa课程、Rasa培训、Rasa面试系列之: Rasa NLU意图和实体-分词器
  20. 威海综合竞争力跃居全国第27位

热门文章

  1. 计算机课可以坐着讲吗,探寻如何把计算机专业课上得更有意思
  2. C语言程序——求学生总成绩和平均成绩
  3. Storm之——使用daemontools监控Storm进程
  4. 绩效考核功能操作流程
  5. Android平台Fastboot介绍
  6. iOS开发之第三方分享微信分享、朋友圈分享,史上最新最全第三方分享微信方式实现、朋友圈方式实现
  7. 单片机温度控制系统课程设计
  8. 【】jQuery 等技术 目录
  9. 求一个数的所有因子(约数)
  10. Win7 Home变旗舰