简体   繁体   English

检查集合中是否存在文档失败

[英]check if Document exist in a collection failed

I'm trying to check if my collection has a document by a function我正在尝试检查我的收藏是否有 function 的文档

this is the function:这是 function:

  @override
  Future<bool> exist(String uid) async {
    final snapShot = await collectionRef.doc(uid).get();
    if (snapShot.exists){
      return true;
    }
    return false;
  }

the result is: Instance of 'Future',, (neither True nor false)结果是:'Future'的实例,,(非真非假)

pointing that my function into Provider将我的 function 指向 Provider

The method is fine, you probably aren't awaiting it when calling it elsewhere.该方法很好,在其他地方调用它时您可能不会等待它。

Also, a slight improvement would be to just return snapShot.exists instead of the if(exists) return true, else false .此外,一个轻微的改进是只return snapShot.exists而不是if(exists) return true, else false

Since it's a future when you call this method like因为当你调用这个方法时它是一个未来

bool something = exist("someId");

Will throw an error saying its an instance of future.会抛出一个错误,说它是一个未来的实例。

But if you await to get a return you should get a bool但是如果你等待得到回报,你应该得到一个布尔值

bool something = await exist("someId");

To check it in a condition在一个条件下检查它

void checkSomething() async{
 if(await exist("someId") == true)
 {
 }
}


[![enter image description here][1]][1]


when quick fix to code M I got these


[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/2HpJM.jpg
  [2]: https://i.stack.imgur.com/XCpW3.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 JS 中的 firestore(不是文档)中是否存在集合 - How do I check if collection exist in firestore (not document) in JS 如何检查firestore数据库中的文档中是否存在字段? - how to check if Field exist in document in firestore database? Flutter firebase 检查 email 存在于 firestore 集合中 - Flutter firebase check email exist inside firestore collection 如何在 collection.document.collection.document() flutter dart 中检查 firebase 中的特定值 - How can I check for particular value in firebase in a collection.document.collection.document() flutter dart 如何检查集合中是否存在文档 (Firebase REACT)? - How do I check if a document exists within a collection (Firebase REACT)? 在 Cloud Firestore 事务中,如果我们不知道文档名称,如何检查文档是否存在于特定集合中 - in Cloud Firestore transaction how Check to see if a Document exists in a specific Collection if we don't know Document name 如何检查 firebase 中的嵌套文档和集合是否存在,如果不存在,如何创建,反应本机 - how to check if nested docs and collection in firebase exist and if don't how to create, in react native “文档集合中未定义的 ID” - "Undefined id in Document Collection" Firebase 检查项目是否存在 - Firebase check item if exist firebase 中的收款文档 ID - Collection Document Id in firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM