skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search