【问题标题】:How to get doc of a collection with specifc condition from cloud firestore如何从 Cloud Firestore 获取具有特定条件的集合的文档
【发布时间】:2020-12-07 19:21:35
【问题描述】:

我正在使用 StreamBuilder 仅从文章集合中获取满足条件的文档。我使用了 where 子句,但它正在获取所有文档并对其进行排序。 (注意:我只想要那些满足条件的文档,而不是全部。) 更新的代码详细信息:使用另一个流构建器从用户集合文档中的以下字段获取 id。

我是怎么做到的:

StreamBuilder<DocumentSnapshot>(
                    stream: FirebaseFirestore.instance
                        .collection('users')
                        .doc(profileScreenController.currentUser.uid)
                        .snapshots(),
                    builder: (context,
                        AsyncSnapshot<DocumentSnapshot> futureSnapShot) {
                      if (!futureSnapShot.hasData &&
                          futureSnapShot.data == null) {
                        return SizedBox();
                      }
                      DocumentSnapshot documentSnapshot =
                          futureSnapShot.data;
                      return Container(
                        height: screenHeight * 0.32,
                        child: StreamBuilder<QuerySnapshot>(
                          stream: FirebaseFirestore.instance
                              .collection('articles')
                              .where('userId',
                                  isEqualTo: documentSnapshot['following'])
                              .snapshots(),
                          builder: (context,
                              AsyncSnapshot<QuerySnapshot> snapshot) {
                            print(documentSnapshot['following']);
                            if (!snapshot.hasData &&
                                snapshot.data == null) {
                              return Center(
                                child: Lottie.asset('assets/loading.json'),
                              );
                            }
                            if (snapshot.data.docs.length == 0) {
                              return Center(
                                child: Column(
                                  mainAxisAlignment:
                                      MainAxisAlignment.center,
                                  children: [
                                    Lottie.asset('assets/no articles.json',
                                        height: 140),
                                    SizedBox(
                                      height: 10,
                                    ),
                                    Text(
                                      'No body yet',
                                      style: TextStyle(
                                        fontSize: 20,
                                        color: Theme.of(context)
                                            .textTheme
                                            .headline1
                                            .color,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }
                            return ListView.builder(
                                shrinkWrap: true,
                                scrollDirection: Axis.horizontal,
                                itemCount: snapshot.data.docs.length,
                                padding: EdgeInsets.only(left: 15),
                                itemBuilder: (context, index) {
                                  selectedIndex = index;
                                  DocumentSnapshot todaysArticles =
                                      snapshot.data.docs[index];
                                  return Container(
                                    width: screenWidth * 0.8,
                                    margin: EdgeInsets.only(right: 6),
                                    decoration: BoxDecoration(
                                      borderRadius:
                                          BorderRadius.circular(30),
                                    ),
                                    child: Text(todaysArticles.data() 
                                            ['title'],
                                        ),
                                  );
                                });
                          },
                        ),
                      );
                    }),

结果:获取两个用户的文档,但不仅是用户名是 Guman 的地方,正如我所说的那样。

我的数据库在文章集合中有 2 个文档:

【问题讨论】:

  • 请编辑问题以更详细地解释什么不符合您的预期。由于我们看不到您传递给where 的变量的值,也看不到您数据库的内容,因此我们无法判断该查询应该返回什么。如果返回的文档与您的期望不符,您应该在此处显示。
  • @DougStevenson 已编辑问题.. 请立即查看 :)
  • 我们仍然看不到您传递的变量的值。 profileScreenController.followerName

标签: flutter dart google-cloud-firestore


【解决方案1】:

我认为这里的问题是profileScreenController.followerName 的值在创建流时为空。

当发送像where('db_field', isEqualTo: null) 这样的查询时,数据库只返回前一个查询中的所有值(在您的情况下是整个集合)。

请在第一次打开流时检查和调试profileScreenController.followerName的值是否不为空。

也许你忘了在某处使用 setState?

【讨论】:

    猜你喜欢
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2021-05-09
    • 2021-03-27
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多