【问题标题】:How to check if subcollection exists in Firestore with Flutter future<bool>如何使用 Flutter future<bool> 检查 Firestore 中是否存在子集合
【发布时间】:2021-01-29 20:45:06
【问题描述】:

如果存在某些子集合,我在从 Firestore 查询中返回 true/false 语句时遇到问题。

这是我的代码:

  Future<bool> checkIfCollectionExist(
      String collectionName, String productId) async {
    await _db
        .collection('products')
        .doc(productId)
        .collection(collectionName)
        .limit(1)
        .get()
        .then((value) {
      return value.docs.isNotEmpty;
    });
  }

结果我得到了Future&lt;bool&gt; 的实例,但我需要true/false 的答案。 我在这里做错了什么?

【问题讨论】:

    标签: flutter google-cloud-firestore future


    【解决方案1】:

    使用

    Future<bool> checkIfCollectionExist(String collectionName, String productId) async {
      var value = await _db
          .collection('products')
          .doc(productId)
          .collection(collectionName)
          .limit(1)
          .get();
      return value.docs.isNotEmpty;
    }
    

    或者

    Future<bool> checkIfCollectionExist(String collectionName, String productId) {
      return _db 
          .collection('products')
          .doc(productId)
          .collection(collectionName)
          .limit(1)
          .get()
          .then((value) {
        return value.docs.isNotEmpty;
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 1970-01-01
      • 2019-01-21
      • 2020-11-17
      • 2019-04-19
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      相关资源
      最近更新 更多