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

dart – Flutter:尝试使用InheritedWidget但是获取Null

发布时间:2020-12-14 14:50:06 所属栏目:百科 来源:网络整理
导读:我正在调用InheritedWidget的函数,但是我的函数返回null,如上所示,我将我的小部件放在树的顶部.该调用来自一个被推入Navigator堆栈的页面,该页面不是此页面.谁知道为什么?我的InheritedWidget代码如下. class LiveKHProvider extends InheritedWidget { fin

Widget Hierarchy


我正在调用InheritedWidget的函数,但是我的函数返回null,如上所示,我将我的小部件放在树的顶部.该调用来自一个被推入Navigator堆栈的页面,该页面不是此页面.谁知道为什么?我的InheritedWidget代码如下.

class LiveKHProvider extends InheritedWidget {
  final LiveKHBloc liveKHBloc = LiveKHBloc();

  @override
  bool updateShouldNotify(InheritedWidget oldWidget) => oldWidget != this;

  static LiveKHBloc of(BuildContext context) {
    var inheritFromWidgetOfExactType =
        context.inheritFromWidgetOfExactType(LiveKHProvider); // to clearly see what’s returning null. 
        //This is where it returns null,//so the below line is executed on a null object.
    return (inheritFromWidgetOfExactType as LiveKHProvider).liveKHBloc;
  }

  LiveKHProvider({Key key,Widget child}) : super(key: key,child: child);
}

解决方法

我通过在InheritedWidget中包装传递给MaterialPageRoute的页面解决了这个问题.我假设树停在页面路线并且没有达到更高,尽管我不是100%肯定.

(编辑:李大同)

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

    推荐文章
      热点阅读