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,但我需要找到更好的解决方案。
最佳答案
Provider 应放置在 widget 树的顶部,位于 MaterialApp 之上
关于flutter - Navigator.push 后上下文丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59317964/