繁体   English   中英

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

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

我做了一些列表视图,它有喜欢和评论部分。 我想显示该帖子有多少喜欢和评论。 类似部分通过显示喜欢它的用户数量来工作,我尝试使用 firestore 中的snapshot.data.docs.length.toString()来显示它,但是当我尝试显示与类似部分具有相同代码的评论数量时,它不会工作,只得到 0。

这是评论区里面这是在我的 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')

with.collection('posts')

在 Streambuilder 的“评论”部分。

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

只需声明生成器的数据类型 arguments.. 我的意思是,生成器:(BuildContext 上下文,AsyncSnapshot 快照)。 检查图像

你能试试这个吗?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM