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

  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 Navigator operation requested with a context that does not include a Navigat

    如下直接在 MaterialApp 中使用 Navigator 是会报 Navigator operation requested with a context that does not inclu ...

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

  4. 报错: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 ...

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

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

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

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

  7. (转)Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误

    问题:电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected excep ...

  8. Could not create the view: An unexpected exception was thrown 异常处理

    MyEclipse 打开后有时候莫名的在server窗口里抛出"Could not create the view: An unexpected exception was thrown&q ...

  9. Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误

    电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected exceptio ...

随机推荐

  1. Actuator Elasticsearch healthcheck error

    1. 相关环境 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  2. PCL已有点类型介绍和增加自定义的点类型

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=265 本小节不仅解释如何增加你自己的PointT点类型,也介绍了PCL中的模 ...

  3. 九十八:CMS系统之登录页面CSRF保护和修改密码页面

    加上CSRF防御 修改密码页面 视图 class ResetPwd(views.MethodView): decorators = [login_required] # 校验登录状态 def get( ...

  4. Elasticsearch常见错误与配置简介

    一.常见错误 1.1 root用户启动elasticsearch报错 Elasticsearch为了安全考虑,不让使用root启动,解决方法新建一个用户,用此用户进行相关的操作.如果你用root启动, ...

  5. 用curl指令访问api-server

    可以直接用curl指令访问api-server,一种是将kubeconfig中所包含的证书抠出来作为参数给curl指令使用.这种方法操作起来比较复杂.还要一种很简单的方法: 首先执行 kubectl ...

  6. Python 面向对象--继承,实现,依赖,关联,聚合,组合

    一. 继承 继承指的是子类继承父类除私有内容以外的其他所有内容, 并且子类具有增加自己新内容的能力. 举例说明: class Animal: print("吃是动物的本能") cl ...

  7. RocketMQ之十:RocketMQ消息接收源码

    1. 简介 1.1.接收消息 RebalanceService:均衡消息队列服务,负责通过MQClientInstance分配当前 Consumer 可消费的消息队列( MessageQueue ). ...

  8. Python3 Selenium自动化web测试 ==> 第七节 WebDriver高级应用 -- 浮动框中,单击选择某个关键字选项

    学习目的: 了解WebDriver的高级应用 正式步骤: 测试Python3代码 # -*- coding:utf-8 -*- from selenium import webdriver from ...

  9. web安全checklist

    web安全漏洞场景分析 输入输出检验不充分 设计缺陷 环境缺陷

  10. ZOJ Problem Set - 1005

    注意,条件:B>=C .应考虑B=C的情况. #include<iostream> using namespace std; int A,B,C; void jugs(int a,i ...