flutter SnackBar异常Another exception was thrown: Scaffold.of() called with a context that does not contain a Scaffold
代码如下:
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Returning Data',
home: HomePage(),
));
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter SnackBar'),
),
body: Center(
child: MaterialButton(
color: Colors.blue,
child: new Text('点我'),
onPressed: () {
final snackBar = new SnackBar(content: new Text('这是一个SnackBar'));
Scaffold.of(context).showSnackBar(snackBar);
},
),
),
);
}
}
当BuildContext在Scaffold之前时,调用Scaffold.of(context)会报错。这时可以通过Builder Widget来解决,代码如下:
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Returning Data',
home: HomePage(),
));
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter SnackBar'),
),
body: Builder(
builder: (BuildContext context) {
return new Center(
child: MaterialButton(
color: Colors.blue,
child: new Text('点我'),
onPressed: () {
final snackBar =
new SnackBar(content: new Text('这是一个SnackBar'));
Scaffold.of(context).showSnackBar(snackBar);
}));
},
),
);
}
}
flutter SnackBar异常Another exception was thrown: Scaffold.of() called with a context that does not contain a Scaffold的更多相关文章
- flutter dialog异常Another exception was thrown: Navigator operation requested with a context that does not include a Navigator
我在使用flutter里的对话框控件的时候遇到了一个奇怪的错误 Another exception was thrown: Navigator operation requested with a c ...
- flutter dialog异常Another exception was thrown: No MaterialLocalizations found
flutter dialog异常Another exception was thrown: No MaterialLocalizations found import 'package:flutter ...
- flutter: Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends State ...
- Could not create the view: An unexpected exception was thrown的解决方法
MyEclipse下面的server窗口突然不能正常显示了,而且还显示Could not create the view: An unexpected exception was thrown(无法创 ...
- Python中的异常(Exception)处理
异常 当你的程序出现例外情况时就会发生异常(Exception).例如,当你想要读取一个文件时,而那个文件却不存在,怎么办?又或者你在程序执行时不小心把它删除了,怎么办?这些通过使用异常来进行处理. ...
- [C#] C# 知识回顾 - 你真的懂异常(Exception)吗?
你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...
- myEclipse Could not create the view: An unexpected exception was thrown.
myEclipse 非正常关闭,打开后 service Explorer or Package Explorer 视图显示不出来.报“Could not create the view: An une ...
- poco json 中文字符,抛异常JSON Exception -->iconv 转换 备忘录。
起因 最近linux服务器通信需要用到json. jsoncpp比较出名,但poco 1.5版本以后已经带有json库,所以决定使用poco::json(linux 上已经用到了poco这一套框架). ...
- Python中获取异常(Exception)信息
异常信息的获取对于程序的调试非常重要,可以有助于快速定位有错误程序语句的位置.下面介绍几种python中获取异常信息的方法,这里获取异常(Exception)信息采用try...except...程序 ...
随机推荐
- mysql5.7.26做主从复制配置
一.首先两台服务器安装好mysql数据库环境 参照linux rpm方式安装mysql5.1 https://www.cnblogs.com/sky-cheng/p/10564604.html 二.主 ...
- 如何在 Visual C# 中执行基本的文件 I/O
演示文件 I/O 操作 本文中的示例讲述基本的文件 I/O 操作.“分步示例”部分说明如何创建一个演示下列六种文件 I/O 操作的示例程序: 注意:如果要直接使用下列示例代码,请注意下列事项: 必须包 ...
- (转) Windows下MySQL免安装版的下载与配置
本人在尊重原著的前提下.针对在实践中所遇到的问题加以整理和完善,如有不足之处,还请各位大神指点江山O(∩_∩)O~ 主要是因为平时自己学习时候会用到.及免安装版本的方便.对于个人开发者挺实用的! 安装 ...
- 递归算法几个实例---C/C++
//1.斐波那契数列 int fibo(int n) { || n==) { ; } else { ) + fibo(n-); } } //2.阶乘 int fac(int n) { || n==) ...
- java8 新特性,stream的应用
https://www.cnblogs.com/fengli9998/p/9002377.html http://www.runoob.com/java/java8-optional-class.ht ...
- PHP三种字符串界定符的区别(单引号,双引号,<<<)
单引号,双引号,<<<的区别如下: 前续:今天突然遇到了<<<EOT,可在运行的时候出错了,所以就度娘了下. 1.单引号:’a string’ \’是唯一的转 ...
- python-pillow图像处理
安装 pip3 install pillow PIL中所涉及的基本概念有如下几个:通道(bands).模式(mode).尺寸(size).坐标系统(coordinate system).调色板(pal ...
- MySQL数据表
创建数据表 CREATE TABLE IF NOT EXISTS ([列名column][类型type][约束可选]) 查看数据表结构 DESC <表名> 修改数据表结构 ALTER ...
- 【leetcode】1209. Remove All Adjacent Duplicates in String II
题目如下: Given a string s, a k duplicate removal consists of choosing k adjacent and equal letters from ...
- Jenkins-ssh远程执行nohup- java无法退出
一,初步 #执行方式 ssh 192.168.2.103 " nohup java -jar /home/a/ipf/ight/feedback/ixxxedback-platform-1. ...