The getter 'data' was called on null , Receiver: null, Tried calling

控制台报错如下:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (26961): The following NoSuchMethodError was thrown building ListeningBuilder(animation: Instance of
I/flutter (26961): 'DetailsInfoProvide', dirty, dependencies: [_InheritedProviders], state: _AnimatedState#34350):
I/flutter (26961): The getter 'data' was called on null.
I/flutter (26961): Receiver: null
I/flutter (26961): Tried calling: data
I/flutter (26961): 
I/flutter (26961): When the exception was thrown, this was the stack:
I/flutter (26961): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
I/flutter (26961): #1      DetailsTopArea.build.<anonymous closure> (package:flutter_shop/pages/details_page/details_top_area.dart:17:66)
I/flutter (26961): #2      Provide.build.<anonymous closure> (package:provide/provide.dart:289:43)
I/flutter (26961): #3      ListeningBuilder.build (package:provide/provide.dart:629:12)
I/flutter (26961): #4      _AnimatedState.build (package:flutter/src/widgets/transitions.dart:148:48)
I/flutter (26961): #5      StatefulElement.build (package:flutter/src/widgets/framework.dart:3824:27)
I/flutter (26961): #6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3735:15)
I/flutter (26961): #7      Element.rebuild (package:flutter/src/widgets/framework.dart:3558:5)
I/flutter (26961): #8      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3715:5)
I/flutter (26961): #9      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3863:11)
I/flutter (26961): #10     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3710:5)
I/flutter (26961): #11     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2954:14)
I/flutter (26961): #12     Element.updateChild (package:flutter/src/widgets/framework.dart:2757:12)
I/flutter (26961): #13     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3746:16)
I/flutter (26961): #14     Element.rebuild (package:flutter/src/widgets/framework.dart:3558:5)
I/flutter (26961): #15     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3715:5)
I/flutter (26961): #16     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3710:5)
I/flutter (26961): #17     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2954:14)
I/flutter (26961): #18     Element.updateChild (package:flutter/src/widgets/framework.dart:2757:12)
I/flutter (26961): #19     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3746:16)
I/flutter (26961): #20     Element.rebuild (package:flutter/src/widgets/framework.dart:3558:5)
I/flutter (26961): #21     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3715:5)
I/flutter (26961): #22     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3710:5)
I/flutter (26961): #23     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2954:14)
I/flutter (26961): #24     Element.updateChild (package:flutter/src/widgets/framework.dart:2757:12)
I/flutter (26961): #25     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4875:14)
I/flutter (26961): #26     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2954:14)
I/flutter (26961): #27     Element.updateChild (package:flutter/src/widgets/framework.dart:2757:12)
I/flutter (26961): #28     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4875:14)
I/flutter (26961): #29     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2954:14)
I/flutter (26961): #30     Element.updateChild (package:flutter/src/widgets/framework.dart:2757:12)
I/flutter (26961): #31     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3746:16)
I/flutter (26961): #32     Element.rebuild (package:flutter/src/widgets/framework.dart:3558:5)
I/flutter (26961): #33     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3715:5)
I/flutter (26961): #34     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3710:5)
I/flutter (26961): #35     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2954:14)
I/flutter (26961): #36     Element.updateChild (package:flutter/src/widgets/framework.dart:2757:12)
I/flutter (26961): #37     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3746:16)
I/flutter (26961): #38     Element.rebuild (package:flutter/src/widgets/framework.dart:3558:5)
I/flutter (26961): #39     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3715:5)
I/flutter (26961): #40     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3710:5)
I/flutter (26961): #41     ParentDataElement.mount (package:flutter/src/widgets/framework.dart:4062:11)
I/flutter (26961): #42     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2954:14)
I/flutter (26961): #43     Element.updateChild (package:flutter/src/widgets/framework.dart:2757:12)
I/flutter (26961): #44     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3746:16)
I/flutter (26961): #45     Element.rebuild (package:flutter/src/widgets/framework.dart:3558:5)
I/flutter (26961): #46     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3715:5)
I/flutter (26961): #47     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3863:11)
I/flutter (26961): #48     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3710:5)
I/flutter (26961): #49     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2954:14)
I/flutter (26961): #50     Element.updateChild (package:flutter/src/widgets/framework.dart:2757:12)
I/flutter (26961): #51     SliverMultiBoxAdaptorElement.updateChild (package:flutter/src/widgets/sliver.dart:1030:36)
I/flutter (26961): #52     SliverMultiBoxAdaptorElement.createChild.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1015:20)
I/flutter (26961): #53     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2251:19)
I/flutter (26961): #54     SliverMultiBoxAdaptorElement.createChild (package:flutter/src/widgets/sliver.dart:1008:11)
I/flutter (26961): #55     RenderSliverMultiBoxAdaptor._createOrObtainChild.<anonymous closure> (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:275:23)
I/flutter (26961): #56     RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:1740:58)
I/flutter (26961): #57     PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:797:15)
I/flutter (26961): #58     RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1740:13)
I/flutter (26961): #59     RenderSliverMultiBoxAdaptor._createOrObtainChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:264:5)
I/flutter (26961): #60     RenderSliverMultiBoxAdaptor.addInitialChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:348:5)
I/flutter (26961): #61     RenderSliverList.performLayout (package:flutter/src/rendering/sliver_list.dart:78:12)
I/flutter (26961): #62     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #63     RenderSliverPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:182:11)
I/flutter (26961): #64     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #65     RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:407:13)
I/flutter (26961): #66     RenderViewport._attemptLayout (package:flutter/src/rendering/viewport.dart:1322:12)
I/flutter (26961): #67     RenderViewport.performLayout (package:flutter/src/rendering/viewport.dart:1240:20)
I/flutter (26961): #68     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #69     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (26961): #70     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #71     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (26961): #72     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #73     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (26961): #74     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #75     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (26961): #76     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #77     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (26961): #78     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #79     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (26961): #80     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #81     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (26961): #82     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #83     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (26961): #84     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #85     _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13)
I/flutter (26961): #86     RenderObject.layout (package:flutter/src/rendering/object.dart:1644:7)
I/flutter (26961): #87     MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:142:11)
I/flutter (26961): #88     _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:431:7)
I/flutter (26961): #89     MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:212:7)
I/flutter (26961): #90     RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:356:14)
I/flutter (26961): #91     RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1519:7)
I/flutter (26961): #92     PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:766:18)
I/flutter (26961): #93     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:336:19)
I/flutter (26961): #94     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:701:13)
I/flutter (26961): #95     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:275:5)
I/flutter (26961): #96     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1012:15)
I/flutter (26961): #97     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:952:9)
I/flutter (26961): #98     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:864:5)
I/flutter (26961): #102    _invoke (dart:ui/hooks.dart:219:10)
I/flutter (26961): #103    _drawFrame (dart:ui/hooks.dart:178:3)
I/flutter (26961): (elided 3 frames from package dart:async)

我这里的原因是由于DetailsInfoProvide这个provide中的请求不是异步,造成了初始时data数据为空,从而导致了控制台异常抛出,而ui界面会先出现瞬间的崩溃然后变为正常。

没有使用异步处理之前的代码:

//发送请求
  getGoodsInfo(String id)  {
    var formData = {
      'goodId': id,
    };
     request('getGoodDetailById', formData: formData).then((val) {
      var responseData = json.decode(val.toString());

      goodsInfo = DetailsModel.fromJson(responseData);
      notifyListeners();
    });
  }

使用异步处理之后的代码为(async--await):

//发送请求  使用async--await
  getGoodsInfo(String id) async {
    var formData = {
      'goodId': id,
    };
    await request('getGoodDetailById', formData: formData).then((val) {
      var responseData = json.decode(val.toString());

      goodsInfo = DetailsModel.fromJson(responseData);
      notifyListeners();
    });
  }
    原文作者:吴封斌博客
    原文地址: https://www.jianshu.com/p/37432f6755cd
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞