如何检查带有Flutter future的Firestore中是否存在子集合<bool>

时间:2020-10-15 11:13:17

标签: flutter google-cloud-firestore future

如果存在某些子集合,我会从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<bool>的实例,但是我需要true / false的答案。 我在这里做错了什么?

1 个答案:

答案 0 :(得分: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;
  });
}