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 StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Center(
child: RaisedButton(
child: Text("Test"),
onPressed: () {
print('click-OK');
Navigator.push(context,
MaterialPageRoute(builder: (context) => SecondPage()));
},
),
),
);
}
}
根据错误信息,错误原因是因为使用的context不包含Navigator实例作为父widget。
也就是在所有的当前用到过的widget中,都没有以Navigator作为父widget的widge。
首先,MaterialApp作为根widget,判断是会能响应跳转页面事件的,其次查看官方文档,看到其中是有navigation相关的,判断MaterialApp是包含Navigator子widget的,能响应跳转事件

那说明在找widget的时候没有在MaterialApp中去找。
查找资料发现
This happens because when you do Navigator.of(context), it will start from the widget associated to the context used. And then go upward in the widget tree until it either find a Navigator or there's no more widget.
这是因为当您执行Navigator.of(context)时,它将从与所使用的context关联的小部件开始。然后在窗口小部件树中向上移动,直到找到导航器或者不再有窗口小部件。
再回到关键性的跳转代码
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondPage())
);
context 上下文 是MyApp的Context,所以直接根本不会在MyApp下的子widget中去找,所以也不可能找到MaterialApp和下面的子widget,而在MyApp上面是没有widget的,MyApp又是咱们自己创建的并没有包含Navigator,所以无法实现跳转。
问题解决
- 方式一:
在MaterialApp下引入一个widget,让Navigator调用该widget的context去找响应跳转的widget
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MainApp',
home: MainPage(),
);
}
}
class MainPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: RaisedButton(
child: Text("Test"),
onPressed: () {
print('click-OK');
Navigator.push(context,
MaterialPageRoute(builder: (context) => SecondPage()));
},
),
);
}
}
方式二:
使用Builder
mport 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Builder(
builder: (context) => Center(
child: RaisedButton(
child: Text("Test"),
onPressed: () {
print('click-OK');
Navigator.push(context,
MaterialPageRoute(builder: (context) => SecondPage()));
},
),
),
),
);
}
}
flutter: Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.的更多相关文章
- 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 Navigator operation requested with a context that does not include a Navigat
如下直接在 MaterialApp 中使用 Navigator 是会报 Navigator operation requested with a context that does not inclu ...
- 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 Da ...
- 报错:flutter: Another exception was thrown: Could not find a generator for route RouteSettings
原因是一个工程中多次使用MaterialApphttps://stackoverflow.com/questions/49132299/could-not-find-a-generator-for-r ...
- flutter dialog异常Another exception was thrown: No MaterialLocalizations found
flutter dialog异常Another exception was thrown: No MaterialLocalizations found import 'package:flutter ...
- myEclipse Could not create the view: An unexpected exception was thrown.
myEclipse 非正常关闭,打开后 service Explorer or Package Explorer 视图显示不出来.报“Could not create the view: An une ...
- (转)Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误
问题:电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected excep ...
- Could not create the view: An unexpected exception was thrown 异常处理
MyEclipse 打开后有时候莫名的在server窗口里抛出"Could not create the view: An unexpected exception was thrown&q ...
- Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误
电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected exceptio ...
随机推荐
- 在CentOS7阿里云服务器部署ThinkPHP5,并配置phpstrom实现同步开发(微信小程序及管理员后端)
小程序和后端同步开发 1.服务器安装tp5框架: 方法很多比如:github.linux命令直接手动下.composer 都可以,方法很多,百度一下,不再累述 2.这时你会发现怎么都访问出现不了这个令 ...
- 02 MySQL之数据表的基本操作
01-创建数据表 # 切换数据库 use test_db; # 创建数据表 语法规则如下: create table 表名 ( 字段名1, 数据类型 [列级别约束条件] [默认值], 字段名2, 数据 ...
- Intel64及IA-32架构优化指南第8章多核与超线程技术——8.9 其它共享资源的优化
8.9 其它共享资源的优化 在多线程应用中的资源优化依赖于处理器拓扑层级内相关联的Cache拓扑以及执行资源.在第7章中讨论了处理器拓扑以及标识处理器拓扑的一种软件算法. 在带有共享总线的平台中,总线 ...
- Node.js express模块 http服务
var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('he ...
- python之scrapy模块下载中间件
知识点 使用方法: 编写一个Downloader Middlewares和我们编写一个pipeline一样,定义一个类,然后在setting中开启 Downloader Middlewares默认的方 ...
- Ubuntu14.04+安卓系统4.3+JDK6编译源码
本博客主要参照: https://www.jianshu.com/p/ecb9c132030f https://blog.csdn.net/gobitan/article/details/243674 ...
- js 中 new call apply bind JSON.stringify 的原理以及模拟实现
1.new的原理和实现 它创建了一个全新的对象. 它会被执行 [[Prototype]](也就是 __proto__)链接. 它使 this指向新创建的对象. 通过 new创建的每个对象将最终被 [[ ...
- 购物车实现 <Block实现回调>
效果图如下: 具体代码实现如下: Model: #import <Foundation/Foundation.h> @interface ShopCarModel : NSObject @ ...
- pandas的用法
1.a = pandas.read_csv(filepath):读取.csv格式的文件到列表a中,文件在路径filepath中 pandas.core.frame.DataFrame是pandas的核 ...
- GitHub项目精选(持续更新)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190815110442972.jpg?x-oss-process=image/watermark,type_Zm ...