异常“类型'String'不是'索引'的类型'int'的子类型”

时间:2020-02-17 07:13:15

标签: firebase exception flutter dart

我正在尝试从DocumentSnapshot访问值,据我所知,该值会返回一个映射。然后,将密钥存储在映射中的列表中,从中构建ListView。然后,我尝试通过迭代使用映射中的键来访问值。尽管我得到了预期的结果,并且UI也按要求呈现,但控制台仍记录了如下异常。 I/flutter ( 2093): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 2093): The following assertion was thrown building: I/flutter ( 2093): type 'String' is not a subtype of type 'int' of 'index' I/flutter ( 2093): I/flutter ( 2093): Either the assertion indicates an error in the framework itself, or we should provide substantially I/flutter ( 2093): more information in this error message to help you determine and fix the underlying cause. I/flutter ( 2093): In either case, please report this assertion by filing a bug on GitHub: I/flutter ( 2093): https://github.com/flutter/flutter/issues/new?template=BUG.md I/flutter ( 2093): I/flutter ( 2093): When the exception was thrown, this was the stack: I/flutter ( 2093): #0 _CartState.cartTile.<anonymous closure> (package:zombie/screens/home/nav/tabs/cart.dart:78:71) I/flutter ( 2093): #1 SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:446:15) I/flutter ( 2093): #2 SliverMultiBoxAdaptorElement._build.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1260:67) I/flutter ( 2093): #3 _HashMap.putIfAbsent (dart:collection-patch/collection_patch.dart:139:29) I/flutter ( 2093): #4 SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1260:26) I/flutter ( 2093): #5 SliverMultiBoxAdaptorElement.createChild.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1273:55) I/flutter ( 2093): #6 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2412:19) I/flutter ( 2093): #7 SliverMultiBoxAdaptorElement.createChild (package:flutter/src/widgets/sliver.dart:1266:11) I/flutter ( 2093): #8 RenderSliverMultiBoxAdaptor._createOrObtainChild.<anonymous closure> (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:354:23) I/flutter ( 2093): #9 RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:1823:58) I/flutter ( 2093): #10 PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:875:15) I/flutter ( 2093): #11 RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1823:13) I/flutter ( 2093): #12 RenderSliverMultiBoxAdaptor._createOrObtainChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:343:5) I/flutter ( 2093): #13 RenderSliverMultiBoxAdaptor.addInitialChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:427:5) I/flutter ( 2093): #14 RenderSliverList.performLayout (package:flutter/src/rendering/sliver_list.dart:78:12) I/flutter ( 2093): #15 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #16 RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:134:11) I/flutter ( 2093): #17 RenderSliverPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:373:11) I/flutter ( 2093): #18 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #19 RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:410:13) I/flutter ( 2093): #20 RenderViewport._attemptLayout (package:flutter/src/rendering/viewport.dart:1367:12) I/flutter ( 2093): #21 RenderViewport.performLayout (package:flutter/src/rendering/viewport.dart:1285:20) I/flutter ( 2093): #22 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #23 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13) I/flutter ( 2093): #24 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #25 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13) I/flutter ( 2093): #26 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #27 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13) I/flutter ( 2093): #28 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #29 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13) I/flutter ( 2093): #30 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #31 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13) I/flutter ( 2093): #32 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #33 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13) I/flutter ( 2093): #34 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #35 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13) I/flutter ( 2093): #36 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #37 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13) I/flutter ( 2093): #38 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #39 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13) I/flutter ( 2093): #40 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #41 RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:105:13) I/flutter ( 2093): #42 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #43 RenderConstrainedBox.performLayout (package:flutter/src/rendering/proxy_box.dart:259:13) I/flutter ( 2093): #44 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #45 RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:744:15) I/flutter ( 2093): #46 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #47 RenderPadding.performLayout (package:flutter/src/rendering/shifted_box.dart:206:11) I/flutter ( 2093): #48 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7) I/flutter ( 2093): #49 MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:163:11) I/flutter ( 2093): #50 _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:477:7) I/flutter ( 2093): #51 MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:232:7) I/flutter ( 2093): #52 RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:391:14) I/flutter ( 2093): #53 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1584:7) I/flutter ( 2093): #54 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:844:18) I/flutter ( 2093): #55 RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:344:19) I/flutter ( 2093): #56 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:774:13) I/flutter ( 2093): #57 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5) I/flutter ( 2093): #58 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1102:15) I/flutter ( 2093): #59 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1041:9) I/flutter ( 2093): #60 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:957:5) I/flutter ( 2093): #64 _invoke (dart:ui/hooks.dart:259:10) I/flutter ( 2093): #65 _drawFrame (dart:ui/hooks.dart:217:3) I/flutter ( 2093): (elided 3 frames from package dart:async) I/flutter ( 2093): ════

以下是导致异常的代码的一部分

Widget cartTile(DocumentSnapshot doc) {
var snap = doc.data;
if(snap == null){
  return SizedBox();
}
List<String> _keys = snap.keys.toList();
print(_keys);
return ListView.builder(
  itemCount: _keys.length,
  physics: BouncingScrollPhysics(),
  itemBuilder: (BuildContext context, int index) {
    return Padding(
      padding: EdgeInsets.fromLTRB(20.0, 3.0, 20.0, 3.0),
      child: Material(
          elevation: 10.0,
          borderRadius: BorderRadius.circular(10.0),
          color: Colors.white,
          child: InkWell(
            borderRadius: BorderRadius.circular(10.0),
            onTap: () async {

            },
            child: Padding(
              padding: const EdgeInsets.fromLTRB(10.0, 0, 10.0, 0),
              child: ListTile(
                leading: CircleAvatar(
                  radius: 25,
                  backgroundColor: Colors.white,
                  backgroundImage: NetworkImage(snap[_keys[index]]['imgPath']),
                ),

很显然,最后一行backgroundImage: NetworkImage(snap[_keys[index]]['imgPath']),是导致异常的原因。我已经尝试解决了几个小时,但无法解决。将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

也许问题出在“ ['imgPath']”附近,您是否尝试仅打印snap[_keys[index]]并查看输出是否为地图?根据例外,似乎是我的清单...