【问题标题】:getting all documents with the same field value firestore flutter获取具有相同字段值的所有文档
【发布时间】:2021-05-24 12:58:51
【问题描述】:

长话短说,我有一个名为 products 的集合,在这个集合中,每个产品都有一个名为 category 的文档,我想获取具有相同类别的所有产品,这就是它的结构。我知道我应该使用where().equalto ,但我没有得到它,有什么帮助吗?

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:
    Future<List<ProductModel>> getProductCategory(String category) async {
        QuerySnapshot query = await _firestore.collection(collection)
            .where("category", isEqualTo: category)
            .get();
        
        if(query.docs.isNotEmpty){
          query.docs.forEach((element) {
            productCategory.add(ProductModel.fromSnapshot(element.data()));
          });
        }
          return productCategory;
      }
    

    这将返回所有类别等于“眼睛”的文档。

    【讨论】:

    • `Future> getProductCategory(String category){ _firestore.collection(collection).where("category",isEqualTo: category).get().then((result) { for(Result.docs 中的 DocumentSnapshot procat){ productCategory.add(ProductModel.fromSnapshot(procat)); } return productCategory; }); } ` 这应该有效吗?
    • 不,它不起作用,我正在更新我的答案,所以你可以直接使用它。
    • @FahmiSawalha 我已经更新了我的代码,请检查一下
    • elemen.data() 出错了,你有不和谐的地方吗?
    • 对不起,我没有意见,你能分享一下云火库依赖的版本吗?所以我会给你快速的解决方案。
    猜你喜欢
    • 2018-07-20
    • 2023-01-12
    • 2018-10-05
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多