Flutter/Firestore - 类型 'List<dynamic>' 不是类型 'Widget' 的子类型

Posted

技术标签:

【中文标题】Flutter/Firestore - 类型 \'List<dynamic>\' 不是类型 \'Widget\' 的子类型【英文标题】:Flutter/Firestore - type 'List<dynamic>' is not a subtype of type 'Widget'Flutter/Firestore - 类型 'List<dynamic>' 不是类型 'Widget' 的子类型 【发布时间】:2020-12-10 15:21:45 【问题描述】:

我正在尝试使用 firestore 作为数据源在垂直列表视图中创建水平列表视图。我已经创建了一个垂直列表视图,现在需要创建水平。这就是我的数据的存储方式:

这是我的代码:

class HomePage extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: const Size.fromHeight(80),
        child: MainAppBar(
          text: 'Pick an occasion...',
        ),
      ),
      body: StreamBuilder(
        stream: Firestore.instance
            .collection('events')
            .orderBy('order')
            .snapshots(),
        builder: buildProductList,
      ),
    );
  


Widget buildProductList(
    BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) 
  if (snapshot.hasData) 
    return ListView.builder(
      scrollDirection: Axis.vertical,
      shrinkWrap: true,
      itemCount: snapshot.data.documents.length,
      itemBuilder: (context, index) 
        DocumentSnapshot user = snapshot.data.documents[index];
        print(user.documentID);
        print(user.data['Occasions']);
        return Column(children: [
          ListTile(
              leading: Image(
                image: FirebaseImage(user.data['img_url']),
                height: 25,
                width: 25,
              ),
              // Access the fields as defined in FireStore
              title: Transform(
                  transform: Matrix4.translationValues(-20, 0.0, 0.0),
                  child: Text(user.documentID))),

          ListView(
            children: [
              user.data['Occasions']
            ],
            scrollDirection: Axis.horizontal,
          )
        ]);
      ,
    );
   else if (snapshot.connectionState == ConnectionState.done &&
      !snapshot.hasData) 
    // Handle no data
    return Center(
      child: Text("No products found."),
    );
   else 
    // Still loading
    return CircularProgressIndicator();
  

这是我收到的错误消息:

I/flutter ( 9398): Birthday
I/flutter ( 9398): [18th, 21st]

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building:
type 'List<dynamic>' is not a subtype of type 'Widget'

When the exception was thrown, this was the stack:
#0      buildProductList.<anonymous closure> (package:easy_tiger/screens/homepage.dart:53:24)
#1      SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:448:22)
#2      SliverMultiBoxAdaptorElement._build.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1136:67)
#3      _HashMap.putIfAbsent (dart:collection-patch/collection_patch.dart:140:29)
#4      SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1136:26)
#5      SliverMultiBoxAdaptorElement.createChild.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1149:55)
#6      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2620:19)
#7      SliverMultiBoxAdaptorElement.createChild (package:flutter/src/widgets/sliver.dart:1142:11)
#8      RenderSliverMultiBoxAdaptor._createOrObtainChild.<anonymous closure> (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:356:23)
#9      RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:1868:58)
#10     PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:920:15)
#11     RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1868:13)
#12     RenderSliverMultiBoxAdaptor._createOrObtainChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:345:5)
#13     RenderSliverMultiBoxAdaptor.addInitialChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:429:5)
#14     RenderSliverList.performLayout (package:flutter/src/rendering/sliver_list.dart:81:12)
#15     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#16     RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:137:11)
#17     RenderSliverPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:377:11)
#18     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#19     RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:471:13)
#20     RenderShrinkWrappingViewport._attemptLayout (package:flutter/src/rendering/viewport.dart:1810:12)
#21     RenderShrinkWrappingViewport.performLayout (package:flutter/src/rendering/viewport.dart:1768:20)
#22     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#23     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#24     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#25     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#26     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#27     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#28     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#29     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#30     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#31     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#32     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#33     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#34     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#35     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#36     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#37     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#38     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#39     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115:13)
#40     RenderObject.layout (package:flutter/src/rendering/object.dart:1769:7)
#41     MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:173:11)
#42     _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:495:7)
#43     MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:242:7)
#44     RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:401:14)
#45     RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1632:7)
#46     PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:889:18)
#47     RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:404:19)
#48     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:867:13)
#49     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:286:5)
#50     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15)
#51     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1056:9)
#52     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:972:5)
#56     _invoke (dart:ui/hooks.dart:253:10)
#57     _drawFrame (dart:ui/hooks.dart:211:3)
(elided 3 frames from dart:async)
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following _TypeError was thrown building:
type 'List<dynamic>' is not a subtype of type 'Widget'

When the exception was thrown, this was the stack: 
#0      buildProductList.<anonymous closure> (package:easy_tiger/screens/homepage.dart:53:24)
#1      SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:448:22)
#2      SliverMultiBoxAdaptorElement._build.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1136:67)
#3      _HashMap.putIfAbsent (dart:collection-patch/collection_patch.dart:140:29)
#4      SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1136:26)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

有人可以帮我解决这个错误吗?这是所需的输出:

谢谢

【问题讨论】:

这只是一个错误的变量分配,例如将一个字符串分配给一个int,关于这个问题有很多问题,控制台会告诉你你在哪里分配了错误的变量,发布53行在您的主页中 【参考方案1】:

在 ListView 中,您必须将 String 列表设置为 Widget 列表。这就是您收到此错误的原因。 而不是使用

ListView(
            children: [
              user.data['Occasions']
            ],
            scrollDirection: Axis.horizontal,
          )

使用

ListView(
            children: [
              for(int i = 0; i< user.data['Occasions'].length; i++) Text(user.data['Occasions'][i]),
            ],
            scrollDirection: Axis.horizontal,
          )

【讨论】:

以上是关于Flutter/Firestore - 类型 'List<dynamic>' 不是类型 'Widget' 的子类型的主要内容,如果未能解决你的问题,请参考以下文章

从 Stream 检索快照时,Flutter/Firestore 返回类型 List<Review> 不是“Map<String, dynamic>”类型的子类型

来自Json的Flutter Firestore返回Null?

Flutter Firestore:从 Firestore 中检索数据并在 Flutter 中显示为小部件

Flutter/cloud-firestore“任务已经完成”异常

Flutter - 任务':cloud_firestore:compileDebugJavaWithJavac'的执行失败

Flutter:[cloud_firestore/unknown] NoSuchMethodError:null 上的无效成员:'includeMetadataChanges'(Flutter Web