简体   繁体   English

FlutterFire - 如果 Firestore 查询 'result.exist' 在有结果时返回 true,那么如果没有结果,返回值是什么?

[英]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.hasErrorquerySnapshot.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.

相关问题 查询多个子集合 firestore flutterfire - Query multiple subcollections firestore flutterfire Firestore - 保存查询结果 - Firestore - Save Query Result 当值不存在时,DynamoDBMapper 的查询方法返回什么? - What does DynamoDBMapper's query method return when value doesn't exist? Firestore 查询有时在循环中不返回结果 - Firestore query doesn't return results sometimes when in a loop Firestore 安全规则:get() 返回与预期结果不同的结果 - Firestore security rules: get() returns different result than the expected one 将 CLGeocoder 结果写入 Firestore - Writing CLGeocoder result to Firestore 如何在 Flutter 中随机执行对 Firestore 的查询和排序结果 - how to perform query to Firestore and order result Randomly in Flutter Firestore SnapshotOptions = { serverTimestamps: 'previous' },如果没有之前的值,它返回什么值? - Firestore SnapshotOptions = { serverTimestamps: 'previous' }, what value it return if there is no previous value? 当存在应返回的数据时,Firestore 查询返回空数组 - Firestore query returns empty array when there is data that should be returned 为什么 persistenceEnabled: true 导致我的查询给出不正确的结果? - Why does persistenceEnabled: true result in my queries giving incorrect results?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM