[英]FlutterFire - If Firestore query 'result.exist' returns a true when there is a result, then what is the return value if no results?
In my database there is a document that has a field bizCode: '123456'.在我的数据库中,有一个文档包含一个字段 bizCode:'123456'。 Running the below returns a TRUE and prints 'code exists'.
运行以下命令返回 TRUE 并打印“代码存在”。
However, when I change isEqualTo: 'abcdef' (which does not exist in my database), nothing is returned or printed out.但是,当我更改 isEqualTo: 'abcdef'(我的数据库中不存在)时,不会返回或打印任何内容。
How should I code it that when the query has no results, it prints returns a false or null or blank and prints 'no such code'?我应该如何编码它,当查询没有结果时,它打印返回一个错误或 null 或空白并打印“没有这样的代码”?
await FirebaseFirestore.instance
.collection("users")
.where('bizCode', isEqualTo: '123456')
.get()
.then((querySnapshot) {
querySnapshot.docs.forEach((result) {
print(result.exists);
if (result.exists) {
print('code exists');
} else {
print('no such code');
}
});
I think you want to check querySnapshot.hasError
, and querySnapshot.size > 0
.我想你想检查
querySnapshot.hasError
和querySnapshot.size > 0
。
I could give you an idea.我可以给你一个主意。 You can check the length of the result.
您可以检查结果的长度。
Do something like this.做这样的事情。
await FirebaseFirestore.instance
.collection("users")
.where('bizCode', isEqualTo: '123456')
.get()
.then((querySnapshot) {
if(querySnapshot.size > 0){
var results = querySnapshot.docs;
results.forEach((result) {
if (result.exists) {
print('code exists');
} else {
print('no such code');
}
}else{
print("No Results Found!");
}
});
Hope this suits your case.希望这适合你的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.