如何使用 Flutter future<bool> 检查 Firestore 中是否存在子集合
How to check if subcollection exists in Firestore with Flutter future<bool>
如果某些子集合存在,我会遇到来自 Firestore 查询的 return 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
答案。
我在这里做错了什么?
使用
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;
});
}
如果某些子集合存在,我会遇到来自 Firestore 查询的 return 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
答案。
我在这里做错了什么?
使用
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;
});
}