简体   繁体   English

snapshot.data.docs.length 不适用于 flutter

[英]snapshot.data.docs.length not working on flutter

I make some listview and it have likes and comments section.我做了一些列表视图,它有喜欢和评论部分。 I want to show how many likes and comments that post had.我想显示该帖子有多少喜欢和评论。 The like section works by displaying the number of users who like it, i try to show it with snapshot.data.docs.length.toString() from firestore but when i try to show how many comments with same code as like section it not working and only get 0.类似部分通过显示喜欢它的用户数量来工作,我尝试使用 firestore 中的snapshot.data.docs.length.toString()来显示它,但是当我尝试显示与类似部分具有相同代码的评论数量时,它不会工作,只得到 0。

this is inside comments field这是评论区里面这是在我的 firestore 里面,评论区

Padding(
    padding: const EdgeInsets.only(top: 8),
    child: Padding(
      padding: const EdgeInsets.only(left: 15),
      child: Row(
        mainAxisAlignment:
            MainAxisAlignment.spaceEvenly,
        children: [
          Container(
            width: 80.0,
            child: Row(
              mainAxisAlignment:
                  MainAxisAlignment.start,
              children: [
                GestureDetector(
                  onLongPress: () {
                    Provider.of<PostFunction>(context,
                            listen: false)
                        .showLikes(
                            context,
                            documentSnapshot
                                .data()['title']);
                  },
                  onTap: () {
                    print('Adding like...');
                    Provider.of<PostFunction>(context,
                            listen: false)
                        .addLike(
                            context,
                            documentSnapshot
                                .data()['title'],
                            Provider.of<AuthenticationService>(
                                    context,
                                    listen: false)
                                .getUserUid);
                  },
                  child: Icon(
                    FontAwesomeIcons.arrowAltCircleUp,
                    color: kGreyColor,
                    size: 18.0,
                  ),
                ),
                StreamBuilder<QuerySnapshot>(
                    stream: FirebaseFirestore.instance
                        .collection('posts')
                        .doc(documentSnapshot
                            .data()['title'])
                        .collection('likes')
                        .snapshots(),
                    builder: (context, snapshot) {
                      if (snapshot.connectionState ==
                          ConnectionState.waiting) {
                        return Center(
                            child:
                                CircularProgressIndicator());
                      } else {
                        return Padding(
                          padding:
                              const EdgeInsets.only(
                                  left: 8.0),
                          child: RichText(
                            text: TextSpan(
                                text: snapshot
                                    .data.docs.length
                                    .toString(),
                                style: GoogleFonts
                                    .openSans(
                                        color:
                                            kGreyColor,
                                        fontSize: 16.0),
                                children: <TextSpan>[
                                  TextSpan(
                                      text: ' votes',
                                      style: GoogleFonts
                                          .openSans(
                                        color:
                                            kGreyColor,
                                        fontSize: 16.0,
                                      )),
                                ]),
                          ),
                        );
                      }
                    })
              ],
            ),
          ),
          SizedBox(width: 20),
          Container(
            width: 150.0,
            child: Row(
              mainAxisAlignment:
                  MainAxisAlignment.start,
              children: [
                GestureDetector(
                  onTap: () {
                    Provider.of<PostFunction>(context,
                            listen: false)
                        .showComments(
                            context,
                            documentSnapshot,
                            documentSnapshot
                                .data()['title']);
                  },
                  child: Icon(
                    FontAwesomeIcons.solidComments,
                    color: kGreyColor,
                    size: 16.0,
                  ),
                ),
                StreamBuilder<QuerySnapshot>(
                    stream: FirebaseFirestore.instance
                        .collection(' posts')
                        .doc(documentSnapshot
                            .data()['title'])
                        .collection('comments')
                        .snapshots(),
                    builder: (context, snapshot) {
                      if (snapshot.connectionState ==
                          ConnectionState.waiting) {
                        return Center(
                            child:
                                CircularProgressIndicator());
                      } else {
                        return Padding(
                          padding:
                              const EdgeInsets.only(
                                  left: 8.0),
                          child: Text(
                            snapshot.data.docs.length
                                .toString(),
                            style: GoogleFonts.openSans(
                                color: kGreyColor,
                                fontSize: 16.0),
                          ),
                        );
                      }
                    }),
              ],
            ),
          ),
          Spacer(),
          Provider.of<AuthenticationService>(context,
                          listen: false)
                      .getUserUid ==
                  documentSnapshot.data()['useruid']
              ? IconButton(
                  icon: Icon(EvaIcons.moreVertical,
                      color: kGreyColor, size: 16),
                  onPressed: () {
                    Provider.of<PostFunction>(context,
                            listen: false)
                        .showPostOptions(
                            context,
                            documentSnapshot
                                .data()['title']);
                  })
              : Container(width: 0.0, height: 0.0)
        ],
      ),
    ),
  ),

Replace.collection(' posts') Replace.collection('posts')

with.collection('posts') with.collection('posts')

in the “comments” section of the Streambuilder.在 Streambuilder 的“评论”部分。

Your stream data is coming as empty as the database cannot find a collection with name (“ posts”).您的 stream 数据为空,因为数据库找不到具有名称的集合(“帖子”)。 So when you try to show how many comments the post had with the same code as the “likes” section it is not working and only getting 0 each time.因此,当您尝试使用与“喜欢”部分相同的代码显示该帖子有多少评论时,它不起作用,每次只能得到 0。

just declare the dataType of builder's arguments..i mean, builder:(BuildContext context,AsyncSnapshot snapShot).只需声明生成器的数据类型 arguments.. 我的意思是,生成器:(BuildContext 上下文,AsyncSnapshot 快照)。 check the image检查图像

Could you please try this one?你能试试这个吗?

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']),
          ),
        );
      },
    );
  },
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试在 listview.buider 中使用 snapshot.data.docs.length 时出现此错误:getter 'docs' 没有为类型 'Object' 定义 - i got this error when i am trying to use snapshot.data.docs.length in listview.buider: The getter 'docs' isn't defined for the type 'Object' 在 Firebase Firestore 中,快照 `size` 与 snapshot.docs `length` 有何不同? - In Firebase Firestore, how is snapshot `size` different from snapshot.docs `length`? Flutter:更新 Firebase 快照数据 - Flutter: update Firebase snapshot data StreamBuilder 快照没有数据 Flutter - StreamBuilder snapshot has no data Flutter 在快照的数据上调用了 getter 'length' - The getter 'length' was called on snapshot's data 在flutter中从firebase的所有文档中获取数据 - get data from all the docs of firebase in flutter Flutter 中的 Firestore 快照侦听器数据重复问题 - Firestore snapshot listener data duplication issue in flutter 在flutter firebase中snapshot.data为空 - snapshot.data is null in flutter firebase 快照在 StreamBuilder flutter 中总是返回空数据 - snapshot returns always empty data in StreamBuilder flutter 获取 firestore 数据库时,“itemCount:snapshot.data.documents.length”不再有效。 有谁知道如何解决这个问题? - when fetching firestore database "itemCount: snapshot.data.documents.length" is not working anymore. does anyone know how to solve this problem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM