简体   繁体   中英

check if Document exist in a collection failed

I'm trying to check if my collection has a document by a function

this is the 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)

pointing that my function into 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM