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 ...
随机推荐
- easyUI之练习
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC & ...
- python设计模式-命令模式
命令模式就是对命令的封装.所谓封装命令,就是将一系列操作封装到命令类中,并且命令类只需要对外公开一个执行方法execute,调用此命令的对象只需要执行命令的execute方法就可以完成所有的操作.这样 ...
- 64位win10系统无法安装.Net framework3.5的解决方法,提示无法连接internet
1)网上有很多办法但是无法解决 2)控制面板>>疑难解答>>系统和安全性>>使用window更新解决问题 再次执行安装即可
- panic 捕获及 throw 崩溃
一,go 语言 panic 报错捕获 使用 go 语言的同学在真实项目中应该经常出现空指针使用等 panic 报错,这类报错与 C++ 中的 try-catch 模块不同,go 语言会一直将当前 p ...
- Java内部类(4):静态内部类&接口内部类
使用static修饰的内部类我们称之为静态内部类,不过我们更喜欢称之为嵌套内部类.静态内部类与非静态内部类之间存在一个最大的区别,我们知道非静态内部类在编译完成之后会隐含地保存着一个引用,该引用是指向 ...
- idea 中的new file 没有jsp
idea 的new file中没有你需要的文件,原因是IDEA认为当前包下不应该创建该文件,以就没有创建该文件的选项. 以jsp 文件为例.其他文件类似. 解决方法: File ---> pro ...
- Chrome浏览器界面截图
常常出现这么一个场景: 网页比较长,需要滚动屏幕才能看完整.这时候如需截屏,则比较麻烦. 如下为解决方法: 推荐Chrome浏览器: 按F12打开调试页面,同时按下ctrl + shift + p, ...
- Python 中__new__()和__init__()的区别
转自: https://blog.csdn.net/weixin_37579123/article/details/89515577 __new__方法:类级别的方法 特性: 1.是在类准备将自身实例 ...
- [转帖]java注解与注释注解区别
https://baijiahao.baidu.com/s?id=1615942718081024481&wfr=spider&for=pc 还需要仔细看一下书的 书里面都有. jav ...
- 【Python】【基础知识】【内置函数】【object的使用方法】
原英文帮助文档: class object Return a new featureless object. object is a base for all classes. It has the ...