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 ...
随机推荐
- Git入门之在IDEA中使用Git上传maven项目
下载安装git客户端: 参考博文:https://www.cnblogs.com/java-maowei/p/5950930.html 在IDEA怎么使用git上传spring的maven项目详解: ...
- SPARQL查询语句整理
本文大多内容来自Joshua Taylor的回答 https://stackoverflow.com/users/1281433/joshua-taylor 查询子类或等价关系 https://sta ...
- java配置SSM框架下的redis缓存
pom.xml引入依赖包 <!--jedis.jar --> <dependency> <groupId>redis.clients</groupId> ...
- Kettle实现从mysql中取2张表数据关联的数据,并写入到mongodb中
1 建立转换,并设置DB连接到mysql 选中DB连接:连接类型选择MySQL,输入主机名称,数据库名称,端口号,用户名,密码 输入连接名称,点击确定.(可以先点击测试,测试一下是否连接成功) 如下图 ...
- Docker在PHP项目开发环境中的应用
http://avnpc.com/pages/build-php-develop-env-by-docker
- 转:OPC协议解析-OPC UA OPC统一架构
1 什么是OPC UA 为了应对标准化和跨平台的趋势,为了更好的推广OPC,OPC基金会近些年在之前OPC成功应用的基础上推出了一个新的OPC标准-OPC UA.OPC UA接口协议包含了之前的 ...
- LCTF (easy-100)
先安装跑一下,不知道为啥我这里模拟器打不开,传到手机上就可以.如下图. 一个输入框,一个按钮,随便输入提示no. 放入JEB反编译. 可以看到有6个Class.大体看一遍,b和e应该和解题无关,在类a ...
- PJzhang:python基础进阶的10个疗程-two
猫宁!!! 第2节:python基本图形绘制 保留字是python基础语法的支撑 默写python代码是件挺恐怖的事情!!! 2008年android操作系统诞生 计算时代 编程语言也是一个江湖 C语 ...
- 01trie
前置芝士 二进制,tire 平衡树 一种数据结构,来维护一些数,需要支持以下操作: 1.插入 xx 数 2.删除 xx 数(若有多个相同的数,因只删除一个) 3.查询 xx 数的排名(排名定义为比当前 ...
- SpringMvc框架 解决在RESTFUL接口后加任意 “.xxx” 绕过权限的问题
问题描述: 框架使用的是SpringMVC.SpringSecurity,在做权限拦截的时候发现一个问题,假设对请求路径/user/detail进行了权限拦截,在访问/user/detail.abc的 ...