flutter - Navigator.push 后上下文丢失
问题描述
Provider.of<IsSpecialist>(context).value
可在此 Navigator.push 上方访问:
Navigator.push(context, MaterialPageRoute(builder: (c) => ChatScreen()));
但在 Navigator.push 之后,它在 ChatScreen 中不可用:
Provider.of<IsSpecialist>(context).value
返回
Could not find the correct Provider above this Widget
为什么上下文被破坏了?如何保持原有的语境?我目前的临时解决方案是在 Navigator 之前从 Provider 获取 isSpecialist 并将其作为参数发送到 ChatScreen,但我需要找到更好的解决方案。
解决方案
上下文不会丢失,您只是使用了错误的上下文。假设A -> B -> C
是小部件层次结构。
我认为您正在推动您的IsSpecialist
atB
并访问它,C
但您使用的是A
.
如果您可以发布更多代码来了解您的小部件层次结构,那就更清楚了。
推荐阅读
- javascript - Vue.js 在现有应用程序上的电子
- centos - 在 CentOS 7 中带有 setuid 的 opentpty()
- react-native - 使用 redux 将状态传递给 stacknavigator
- c++ - 提升精神词位及其属性
- sql - 具有 REGEX 模式的 1000 条案例语句的高效字段映射
- excel - 将带空格的命令行参数传递给 Excel
- forms - 访问 - 控制会议记录的表格
- java - 双精度不能转换为字符串
- kinect - Kinect for Windows 版本 1 文档
- javascript - 使用 JQuery 的两个 div 之间的水平滑动效果