代码如下:

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的更多相关文章

  1. 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 ...

  2. flutter dialog异常Another exception was thrown: No MaterialLocalizations found

    flutter dialog异常Another exception was thrown: No MaterialLocalizations found import 'package:flutter ...

  3. 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 ...

  4. Could not create the view: An unexpected exception was thrown的解决方法

    MyEclipse下面的server窗口突然不能正常显示了,而且还显示Could not create the view: An unexpected exception was thrown(无法创 ...

  5. Python中的异常(Exception)处理

    异常 当你的程序出现例外情况时就会发生异常(Exception).例如,当你想要读取一个文件时,而那个文件却不存在,怎么办?又或者你在程序执行时不小心把它删除了,怎么办?这些通过使用异常来进行处理. ...

  6. [C#] C# 知识回顾 - 你真的懂异常(Exception)吗?

    你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...

  7. myEclipse Could not create the view: An unexpected exception was thrown.

    myEclipse 非正常关闭,打开后 service Explorer or Package Explorer 视图显示不出来.报“Could not create the view: An une ...

  8. poco json 中文字符,抛异常JSON Exception -->iconv 转换 备忘录。

    起因 最近linux服务器通信需要用到json. jsoncpp比较出名,但poco 1.5版本以后已经带有json库,所以决定使用poco::json(linux 上已经用到了poco这一套框架). ...

  9. Python中获取异常(Exception)信息

    异常信息的获取对于程序的调试非常重要,可以有助于快速定位有错误程序语句的位置.下面介绍几种python中获取异常信息的方法,这里获取异常(Exception)信息采用try...except...程序 ...

随机推荐

  1. 设备树里面#address-cells 、#size-cells、reg三者的关系

    栗子1: cpus { #address-cells = <>; #size-cells = <>; cpu@ { compatible = "arm,cortex- ...

  2. Interviewe HDU - 3486 (ST表+枚举 )(非二分,看下这个数据、)

    YaoYao has a company and he wants to employ m people recently. Since his company is so famous, there ...

  3. 二、Ubuntu16.04安装搜狗wps

    1.搜狗: 安装搜狗输入法,下载地址:http://pinyin.sogou.com/linux/(搜索官网下载及安装方法),deb安装方法类似Windows的exe安装,安装后重启生效. 2.wps ...

  4. Kendo UI使用教程:Bower Packages

    [Kendo UI最新试用版下载] Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support for React和 ...

  5. big.js常用方法

    官网api:http://mikemcl.github.io/big.js/ +:minus.minus(n) ⇒ Big  %:mod.mod(n) ⇒ Big -:plus.plus(n) ⇒ B ...

  6. 源于react里面constructor()和super()的使用

    es5里面没有继承的话 //构造函数 function People(name,age){ this.age = age; this.name = name } let p1 = new People ...

  7. 【leetcode】1184. Distance Between Bus Stops

    题目如下: A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between al ...

  8. 类型xxx 无法反序列化。缺乏对应的数据成员。

    WebApi——json返回多了 k_BackingField   产生原因: model类添加了    [System.Serializable] 返回json的时候会出现.

  9. PROP_ENTRY_TYPE用法

    最近几天刚换了vs2019,然后各种水土不服 这不 刚建了一个ATL组件,发现添加了属性编译不过,提示: 错误 C4995 “PROP_ENTRY”: 名称被标记为 #pragma deprecate ...

  10. 关于 ATL 中 CComControl 的构造

    分享一篇 C++语言 & ATL 的高阶解读笔记,你需要在C++语言特性中上串下跳,应该算篇有质量的文章. class ATL_NO_VTABLE CHello : // ... public ...