snapshot.data.docs.length 不适用于颤振

时间:2021-06-09 10:41:09

标签: firebase flutter dart google-cloud-firestore

我制作了一些列表视图,它有喜欢和评论部分。我想显示该帖子有多少赞和评论。喜欢部分通过显示喜欢它的用户数量来工作,我尝试使用来自 firestore 的 feature_maps = model.predict(img) df = pd.DataFrame(feature_maps) df.to_csv(filename, index=False) 来显示它,但是当我尝试显示与喜欢部分具有相同代码的评论数量时,它不起作用并且只得到 0。

这是在评论字段内 this is inside my firestore, comments field

snapshot.data.docs.length.toString()

2 个答案:

答案 0 :(得分:1)

更换 .collection('帖子')

与 .collection('posts')

在 Streambuilder 的“评论”部分。

您的流数据变得空空如也,因为数据库找不到具有名称(“posts”)的集合。 因此,当您尝试使用与“喜欢”部分相同的代码显示帖子有多少评论时,它不起作用,每次只能获得 0。

答案 1 :(得分:0)

你可以试试这个吗

StreamBuilder( stream: firestoreDB, builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { if (!snapshot.hasData) { return Center(child: CircularProgressIndicator()); } return ListView.builder( itemCount: snapshot.data!.size, itemBuilder: (context, index) { return Card( child: ListTile( title: Text(snapshot.data!.docs[index]['name']), subtitle: Text(snapshot.data!.docs[index]['description']), ), ); }); }, ),