flutter 最容易报的一个错误就是does not include,因为其思想是组合

这种情况即使是外面包裹了materialapp也是无效的,因为flutter会根据这个context一直上溯,一直到根节点的widget,注意,上溯是根据context的,会上溯到这个context相关的widget的最根节点

14down vote

This error is unrelated to the destination. It happens because you used a context that doesn’t contain a Navigator instance as parent.

How do I create a Navigator instance then ?

This is usually done by inserting in your widget tree a MaterialApp or WidgetApp. Although you can do it manually by using Navigator directly but less recommended. Then, all children of such widget can access NavigatorState using Navigator.of(context).

Wait, I already have a MaterialApp/WidgetApp !

That’s most likely the case. But this error can still happens when you use a context that is a parent of MaterialApp/WidgetApp.

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.

In the first case, everything is fine. In the second, it throws a

Navigator operation requested with a context that does not include a Navigator.

So, how do I fix it ?

First, let’s reproduce this error :

import 'package:flutter/material.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(home: Center(child: RaisedButton(child: Text("Foo"),onPressed: () => Navigator.pushNamed(context, "/"),),),);}
}

This example creates a button that attempts to go to ‘/’ on click but will instead throw an exception.

Notice here that in the

onPressed: () => Navigator.pushNamed(context, “/”),
we used context passed by to build of MyApp.

The problem is, MyApp is actually a parent of MaterialApp. As it’s the widget who instantiate MaterialApp! Therefore MyApp’s BuildContext doesn’t have a MaterialApp as parent!

To solve this problem, we need to use a different context.

In this situation, the easiest solution is to introduce a new widget as child of MaterialApp. And then use that widget’s context to do the Navigator call.

There are a few ways to achieve this. You can extract home into a custom class :

import 'package:flutter/material.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(home: MyHome());}
}class MyHome extends StatelessWidget {@overrideWidget build(BuildContext context) {return Center(child: RaisedButton(child: Text("Foo"),onPressed: () => Navigator.pushNamed(context, "/"),),);}
}

Or you can use Builder :

import 'package:flutter/material.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(home: Builder(builder: (context) => Center(child: RaisedButton(child: Text("Foo"),onPressed: () => Navigator.pushNamed(context, "/"),),),),);}
}

Flutter报错 Navigator operation requested with a context that does not include a Navigator.相关推荐

  1. Flutter提示之Navigator operation requested with a context that does not include a Navigator.

    1 .问题 用Flutter写了页面跳转,提示错误如下 Navigator operation requested with a context that does not include a Nav ...

  2. flutter报错Navigator operation requested with a context that does not include a Navigator

    此问题一般情况出现在直接在main.dart文件中操作路由. 如上情况提示路由控制器需要一个context但是当前navigator并不包含,通俗的讲要使用路由(Navigator),根控件不能直接是 ...

  3. 【错误记录】Flutter 界面跳转报错 ( Navigator operation requested with a context that does not include a Naviga )

    文章目录 一.报错信息 二.问题分析 三.解决方案 一.报错信息 Flutter 界面跳转时 , 报如下错误 : ======== Exception caught by gesture ====== ...

  4. Flutter报错BlocProvider.of() called with a context that does not contain a Bloc of type **.

    Flutter报错BlocProvider.of() called with a context that does not contain a Bloc of type **. 在使用flutter ...

  5. flutter报错Could not connect to lockdownd, error code -

    关于 flutter 报错信息解决方案 第一步: cmd+shift+g 前往 /var/db 文件夹,找到lockdown文件夹,修改读写权限 第二步 : 打开命令行,依次执行 brew updat ...

  6. php7链接数据库报错The server requested authentication method unknown to the client

    php7链接数据库报错The server requested authentication method unknown to the client 主要原因 是因为 mysql 8.0 升级了密码 ...

  7. RedisTemplate报错WRONGTYPE Operation against a key holding the wrong kind of value

    场景 今天写业务代码,先从redis中读取值,不存在的话加redis锁,锁定之后再次尝试读取一次redis,没有值的话去db中读取并写redis缓存. 非常简单的代码,确报错WRONGTYPE Ope ...

  8. SVN报错“previous operation has not finished…“的解决办法

    SVN因多次操作卡死,报错"previous operation has not finished-", 无法进行任何新操作时,可以在SVN的工作目录下打开命令行窗口,输入&quo ...

  9. flutter 报错java.net.URISyntaxException: Illegal character in opaque part at index 2

    记录flutter报错如下错误以及解决办法 FAILURE: Build failed with an exception. * What went wrong: Execution failed f ...

  10. 搭建网站服务器时报错url,服务器网站总报错“ERROR the requested URL could not be retrieved”解决办法...

    服务器网站总报错"ERROR the requested URL could not be retrieved" ERROR The requested URL could not ...

最新文章

  1. 分页存储过程(使用row_number()) Sql Server
  2. Spring Cloud Gateway 入门
  3. 如何:重写 ToString 方法(C# 编程指南)
  4. 私有云Opetstack的创建与运用
  5. spring配置xml遇到的一些错误,最后修改的版本
  6. Eclipse导入GitHub项目两处报错处理
  7. 用与非门设计血型配对电路_电路板中与非门集成电路的控制原理
  8. 路由器的异步和同步串行接口
  9. 【异常】INFO: TopologyManager: EndpointListener changed ...
  10. 深度学习2.0-23.Keras高层接口之CIFAR10自定义网络实战
  11. 经典机器学习系列(四)【神经网络详解】
  12. 重新安装windows中已删除的Wireless Zero Configuration(WZC)服务
  13. 四、OSPF配置实验
  14. SSM项目源码基于SSM实现的小说网站含前后台
  15. itunes备份是整个手机备份吗_如何使用iTunes备份手机资料
  16. C/C++ Linux 键盘检测
  17. 战场管理系统的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  18. 为什么阿里那么难进,原来精髓在这
  19. pcf85263 linux驱动源码,PCF85263AT/AJ
  20. 如何下载宝坻区卫星地图高清版大图?

热门文章

  1. cogs 1811. [NOIP2014]螺旋矩阵
  2. ubuntu17.04新安装之后的软件准备
  3. Ubuntu的常识使用了解2
  4. log4net 不生成日志文件的解决办法
  5. keras 的层,中文
  6. 值得拥有的手绘风格画图工具
  7. 论文赏析[EMNLP18]用控制任务来设计并解释探测任务
  8. [TACL17]基于中序转移的成分句法分析
  9. 数据预处理—8.属性归约之主成分分析(理论及python实现)
  10. 机器学习—XGBoost实战与调参