0

我试图做的是用它需要的必要数据填充一张卡片,这些数据是我的用户集合中的 userImage 和 UserFirstname 以及我的 carInfo 集合中的子集合(carReviews)中的数据,我存储必要的评论数据: subcollection(carReviews)

用户集合

StreamBuilder<QuerySnapshot>(
                              stream: FirebaseFirestore.instance
                                  .collection('carInfo')
                                  .doc(snapshot.data!.docs[index]
                                      .get('CaradvId'))
                                  .collection('carReviews')
                                  .snapshots(),
                              builder: (context, snapshot5) {
                                return StreamBuilder<QuerySnapshot>(
                                    stream: FirebaseFirestore.instance
                                        .collection('users')
                                        .where('uid',
                                            isEqualTo: snapshot5
                                                .data!.docs[index]
                                                .get('reviewerId'))
                                        .snapshots(), 
                                    builder: (context, snapshot6) {
                                      return Container(
                                        padding: EdgeInsets.symmetric(
                                            horizontal: 5),
                                        height: MediaQuery.of(context)
                                                .size
                                                .height *
                                            0.225,
                                            child: ListView.builder(
                                                shrinkWrap: true,
                                                scrollDirection:
                                                    Axis.horizontal,
                                                itemCount: snapshot5
                                                    .data!.docs.length,
                                                itemBuilder:
                                                    (BuildContext context,
                                                        int index) {
                                                         
                                                  DateTime date = snapshot5
                                                      .data!.docs[index]
                                                      .get('reviewDate')
                                                      .toDate();
                                                  String dateString =
                                                      DateFormat(
                                                              'dd MMM yyyy')
                                                          .format(date);

                                                  return ConstrainedBox(

当我打印 snapshot5.data!.docs[index].get('reviewerId') 时,我得到了两个 id,他们已经查看了预期的内容,但是 snapshot6.data!.docs.length 为 1,这没有任何意义!我已经尝试同时使用 snapshot6.data!.docs.length 和 snapshot5.data!.docs.length 作为 itemcount 如果我使用 snapshot6.data!.docs.length 我只会在列表中得到一个项目而没有错误,如果我使用snapshot5.data!.docs.length 我得到范围错误,因为它通过了所有只有 2 的评论。如果 statment 不会改变我尝试过的输出,我使用“hasdata”。

我使用 snapshot5.data!.docs.length 时的样子

我使用 snapshot6.data!.docs.length 时的样子

Card(
                                                        elevation: 2,
                                                        child: Container(
                                                          child: Column(
                                                            mainAxisAlignment:
                                                                MainAxisAlignment
                                                                    .start,
                                                            children: [
                                                              Row(
                                                                mainAxisAlignment:
                                                                    MainAxisAlignment
                                                                        .start,
                                                                children: [
                                                                  Container(
                                                                    padding: EdgeInsets.only(
                                                                        left:
                                                                            12.5,
                                                                        top:
                                                                            12.5),
                                                                    height:
                                                                        55,
                                                                    width:
                                                                        55,
                                                                    child:
                                                                        CircleAvatar(
                                                                      backgroundImage:
                                                                          NetworkImage(
                                                                        snapshot6.data!.docs[index].get('userImage'),
                                                                      ), //userimage
                                                                      radius:
                                                                          40,
                                                                    ),
                                                                  ),
                                                                  Container(
                                                                      padding: EdgeInsets.only(
                                                                          top:
                                                                              15),
                                                                      child:
                                                                          Column(
                                                                       crossAxisAlignment:
                                                        CrossAxisAlignment
                                                            .start,
                                                                        children: [
                                                                          Container(  padding: EdgeInsets.only(
                                                                          left:
                                                                              5),child:
                                                                            RatingBar.builder(
                                                                              initialRating: double.parse(snapshot5.data!.docs[index].get('reviewRating')),
                                                                              minRating: 1,
                                                                              direction: Axis.horizontal,
                                                                              allowHalfRating: true,
                                                                              updateOnDrag: false,
                                                                              itemCount: 5,
                                                                              itemSize: 14,
                                                                              itemPadding: EdgeInsets.symmetric(vertical: 0),
                                                                              itemBuilder: (context, _) => Icon(
                                                                                Icons.star,
                                                                                color: Colors.amber,
                                                                              ),
                                                                              onRatingUpdate: (rating) {
                                                                                print(rating);
                                                                              },
                                                                            ),
                                                                          ),
                                                                          SizedBox(height: 7.5),
                                                                          Row(
                                                                            children: [
                                                                              Container(
                                                                                padding: EdgeInsets.only(
                                                                                  left: 7.5,
                                                                                ),
                                                                                child: Text(
                                                                                  snapshot6.data!.docs[index].get('userFirstname'),
                                                                                  overflow: TextOverflow.ellipsis,
                                                                                  maxLines: 1,
                                                                                  style: TextStyle(
                                                                                    fontSize: 10,
                                                                                    fontWeight: FontWeight.w800,
                                                                                    color: Colors.black87,
                                                                                  ),
                                                                                ),
                                                                              ),
                                                                              Container(
                                                                                  padding: EdgeInsets.only(left: 2.5),
                                                                                  child: Text(
                                                                                    dateString,
                                                                                    overflow: TextOverflow.ellipsis,
                                                                                    maxLines: 1,
                                                                                    style: TextStyle(
                                                                                      fontSize: 10,
                                                                                      fontWeight: FontWeight.w800,
                                                                                      color: Colors.black54,
                                                                                    ),
                                                                                  ))
                                                                            ],
                                                                          )
                                                                        ],
                                                                      )),
                                                                ],
                                                              ),
                                                              Container( padding: EdgeInsets.only(
                                                                        left:
                                                                            12.5,
                                                                        top:
                                                                            12.5),
                                                                height: 130,width: MediaQuery.of(
                                                                    context)
                                                                .size
                                                                .width *
                                                            0.75,
                                                                child: Text(snapshot5
                                                                    .data!
                                                                    .docs[
                                                                        index]
                                                                    .get(
                                                                        'reviewContent')),
                                                              )
                                                            ],
                                                          ),
                                                        )),
4

0 回答 0