加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

dart – Flutter:从没有上下文的InheritedWidgets访问数据?

发布时间:2020-12-14 14:58:37 所属栏目:百科 来源:网络整理
导读:我看到我可以在build()方法中访问InheritedWidgets,如下所示:final inheritedWidget = ChronoApp.of(context);但是如果我想在其他地方访问它,比如在initState()中没有上下文.我该怎么做? 解决方法 根据此文档,上下文应该在initState中使用上下文getter. ht
我看到我可以在build()方法中访问InheritedWidgets,如下所示:final inheritedWidget = ChronoApp.of(context);但是如果我想在其他地方访问它,比如在initState()中没有上下文.我该怎么做?

解决方法

根据此文档,上下文应该在initState中使用上下文getter.

https://docs.flutter.io/flutter/widgets/State/context.html

The framework associates State objects with a BuildContext after creating them with StatefulWidget.createState and before calling initState.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读