小部件库捕获的异常

Posted

技术标签:

【中文标题】小部件库捕获的异常【英文标题】:Exception Caught by widgets library 【发布时间】:2019-06-13 17:25:58 【问题描述】:

从 API 获取数据并使用 ListView 列出数据时出现异常

当我给 itemCount 的数据少于获取的总数据时,它可以正常工作,并且在没有任何无限滚动的情况下显示少一个元素,否则它会在无限滚动时显示错误,这里是代码:

Expanded(
              child: StreamBuilder(
                initialData: 'https://www.triply.fun/tc-api/adpepv/tickets_info/20/1/',
                  stream: apiURL,
                  builder: (context, snapshotVal) 
                    return Container(
                        child: FutureBuilder<List<TicketsInfo>>(
                            future: ticketInfo(snapshotVal.data),
                            builder: (context, snapshot) 
                              return (snapshot.hasData &&
                                      snapshot.data.length != null && snapshot.data.length > 0 )
                                  ? ListView.builder(
                                      scrollDirection: Axis.vertical,
                                      shrinkWrap: true,
                                      padding: EdgeInsets.only(bottom: 20),
                                      itemBuilder: (context, int index) 
                                        return GestureDetector(
                                          onTap: () => Navigator.pushNamed(
                                              context, "/Detail"),
                                          child: Column(
                                            children: <Widget>[
                                              Container(
                                                height: 1,
                                                color: Colors.black12,
                                              ),
                                              Container(
                                                  padding:
                                                      EdgeInsets.all(0),
                                                  margin: EdgeInsets.all(0),
                                                  child: Row(
                                                    children: <Widget>[
                                                      Expanded(
                                                        child: Container(
                                                          padding: EdgeInsets
                                                              .symmetric(
                                                                  horizontal:
                                                                      20),
                                                          alignment: Alignment
                                                              .centerLeft,
                                                          child: Text(
                                                            "$snapshot.data[index].data.buyerFirst $snapshot.data[index].data.buyerFirst",
                                                            style: textStyle(
                                                                kColor_dark_text,
                                                                FontWeight
                                                                    .w400,
                                                                16),
                                                          ),
                                                          height: 74,
                                                        ),
                                                      ),
                                                      Container(
                                                        width: 40,
                                                        child: Image.asset(
                                                          ic_forward,
                                                          fit: BoxFit
                                                              .contain,
                                                          height: 10,
                                                          width: 6,
                                                        ),
                                                      ),
                                                      Padding(
                                                          padding: EdgeInsets
                                                              .only(
                                                                  left: 10))
                                                    ],
                                                  )),
                                            ],
                                          ),
                                        );
                                      ,
                                      itemCount: snapshot.data.length)
                                  : Center(
                                      child: CircularProgressIndicator(
                                          valueColor:
                                              AlwaysStoppedAnimation<Color>(
                                                  kColor_green_theme)));
                            ));
                  ),
            )

这是我得到的运行错误:

I/flutter (17054): url 得到了https://www.triply.fun/tc-api/adpepv/tickets_info/20/1/ I/flutter (17054):数据获取 ['TicketsInfo' 实例、'TicketsInfo' 实例、'TicketsInfo' 实例、'TicketsInfo' 实例、'TicketsInfo' 实例、'TicketsInfo' 实例、'TicketsInfo' 实例','TicketsInfo' 的实例,'TicketsInfo' 的实例,'TicketsInfo' 的实例,'TicketsInfo' 的实例] I/flutter (17054):══╡ 小部件库发现异常╞══════════════════════════════════════════════════════ ═════════════════════════ I/flutter (17054):以下 NoSuchMethodError 被抛出构建: I/flutter (17054):在 null 上调用了 getter 'buyerFirst'。 I/颤振(17054):接收器:空 I/flutter (17054):尝试调用:buyerFirst 我/颤振(17054): I/flutter (17054):抛出异常时,这是堆栈: I/flutter (17054): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5) I/flutter (17054): #1 _ListScreenState.build... (package:check_in/src/screens/list_screen.dart:141:94) I/flutter (17054): #2 SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:397:15) I/flutter (17054):#3 SliverMultiBoxAdaptorElement._build。 (包:flutter/src/widgets/sliver.dart:1000:67) I/flutter (17054): #4 _HashMap.putIfAbsent (dart:collection/runtime/libcollection_patch.dart:143:29) I/flutter (17054): #5 SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1000:26) I/flutter (17054):#6 SliverMultiBoxAdaptorElement.createChild。 (包:flutter/src/widgets/sliver.dart:1013:55) I/flutter (17054): #7 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2266:19) I/flutter (17054): #8 SliverMultiBoxAdaptorElement.createChild (package:flutter/src/widgets/sliver.dart:1006:11) I/flutter (17054):#9 RenderSliv​​erMultiBoxAdaptor._createOrObtainChild。 (包:flutter/src/rendering/sliver_multi_box_adaptor.dart:274:23) I/flutter (17054):#10 RenderObject.invokeLayoutCallback。 (包:flutter/src/rendering/object.dart:1730:58) I/flutter (17054): #11 PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:799:15) I/flutter (17054): #12 RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1730:13) I/flutter (17054): #13 RenderSliv​​erMultiBoxAdaptor._createOrObtainChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:263:5) I/flutter (17054): #14 RenderSliv​​erMultiBoxAdaptor.insertAndLayoutChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:401:5) I/flutter (17054): #15 RenderSliv​​erList.performLayout.advance (package:flutter/src/rendering/sliver_list.dart:190:19) I/flutter (17054): #16 RenderSliv​​erList.performLayout (package:flutter/src/rendering/sliver_list.dart:233:19) I/flutter (17054): #17 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7) I/flutter (17054): #18 RenderSliv​​erPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:182:11) I/flutter (17054): #19 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7) I/flutter (17054): #20 RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:405:13) I/flutter (17054): #21 RenderShrinkWrappingViewport._attemptLayout (package:flutter/src/rendering/viewport.dart:1645:12) I/flutter (17054): #22 RenderShrinkWrappingViewport.performLayout (package:flutter/src/rendering/viewport.dart:1608:20) I/flutter (17054): #23 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7) I/flutter (17054): #24 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13) I/flutter (17054): #25 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7) I/flutter (17054): #26 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13) I/flutter (17054): #27 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7) I/flutter (17054): #28 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13) I/flutter (17054): #29 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7) I/flutter (17054): #30 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13) I/flutter (17054): #31 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7) I/flutter (17054): #32 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13) I/flutter (17054): #33 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7) I/flutter (17054): #34 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13) I/flutter (17054): #35 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7) I/flutter (17054): #36 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13) I/flutter (17054): #37 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7) I/flutter (17054): #38 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13) I/flutter (17054): #39 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7) I/flutter (17054): #40 RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:798:17) I/flutter (17054): #41 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1509:7) I/flutter (17054): #42 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:768:18) I/flutter (17054): #43 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:281:19) I/flutter (17054): #44 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:677:13) I/flutter (17054): #45 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5) I/flutter (17054): #46 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15) I/flutter (17054): #47 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9) I/flutter (17054): #48 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5) I/flutter (17054): #49 _invoke (dart:ui/hooks.dart:154:13) I/flutter (17054): #50 _drawFrame (dart:ui/hooks.dart:143:3) 我/颤振(17054):══════════════════════════════════════════════════════════ ══════════════════════════════════════════════════ ═══════

【问题讨论】:

The getter 'buyerFirst' was called on null. 【参考方案1】:

您可以删除buyerFirst之前的数据,然后重试:

$snapshot.data[index]**.data**.buyerFirst
$snapshot.data[index].data.buyerFirst

替换为:

$snapshot.data[index].buyerFirst $snapshot.data[index].buyerFirst

【讨论】:

以上是关于小部件库捕获的异常的主要内容,如果未能解决你的问题,请参考以下文章

小部件库捕获的异常

小部件库捕获的异常,在颤振中抛出以下断言构建答案**错误**

如何在颤振小部件测试中捕获来自未来的错误?

***Error 是由我的自定义底页小部件引发的

flutter ios 无法运行,获取小部件库异常和设备连接丢失

找不到正确的提供者