flutter 微件库捕获异常

whlutmcx  于 2023-01-27  发布在  Flutter
关注(0)|答案(1)|浏览(120)

从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 got https://www.triply.fun/tc-api/adpepv/tickets_info/20/1/
I/flutter (17054): Data fetched [Instance of 'TicketsInfo', Instance of 'TicketsInfo', Instance of 'TicketsInfo', Instance of 'TicketsInfo', Instance of 'TicketsInfo', Instance of 'TicketsInfo', Instance of 'TicketsInfo', Instance of 'TicketsInfo', Instance of 'TicketsInfo', Instance of 'TicketsInfo', Instance of 'TicketsInfo']
I/flutter (17054): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (17054): The following NoSuchMethodError was thrown building:
I/flutter (17054): The getter 'buyerFirst' was called on null.
I/flutter (17054): Receiver: null
I/flutter (17054): Tried calling: buyerFirst
I/flutter (17054): 
I/flutter (17054): When the exception was thrown, this was the stack:
I/flutter (17054): #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
I/flutter (17054): #1      _ListScreenState.build.<anonymous closure>.<anonymous closure>.<anonymous closure> (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.<anonymous closure> (package: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.<anonymous closure> (package: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      RenderSliverMultiBoxAdaptor._createOrObtainChild.<anonymous closure> (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:274:23)
I/flutter (17054): #10     RenderObject.invokeLayoutCallback.<anonymous closure> (package: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     RenderSliverMultiBoxAdaptor._createOrObtainChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:263:5)
I/flutter (17054): #14     RenderSliverMultiBoxAdaptor.insertAndLayoutChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:401:5)
I/flutter (17054): #15     RenderSliverList.performLayout.advance (package:flutter/src/rendering/sliver_list.dart:190:19)
I/flutter (17054): #16     RenderSliverList.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     RenderSliverPadding.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)
I/flutter (17054): ════════════════════════════════════════════════════════════════════════════════════════════════════
lmvvr0a8

lmvvr0a81#

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

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

改为:

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

相关问题