Home > Net >  check if Document exist in a collection failed
check if Document exist in a collection failed

Time:08-14

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

CodePudding user response:

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.

CodePudding user response:

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
  • Related