Flutter returning a bool type from a Future<bool> Method

10,725

The return type returned from doesNameAlreadyExist is Future<bool>, so the line doesNameAlreadyExist("userName", usernameController.value) == true, is actually Future<bool> == bool. You need to await, or then the result.

doesNameAlreadyExist("userName", usernameController.value).then(value => value == true)

or

(await doesNameAlreadyExist("userName", usernameController.value)) == true

Read more about async programming here: Dart Futures

Share:
10,725

Related videos on Youtube

theSimm
Author by

theSimm

Updated on May 25, 2022

Comments

  • theSimm
    theSimm almost 2 years

    This question is quite similar to this but the explanation didn't quite help for my use-case. I have a method of type Future that return a bool performing a query to cloud firestore to check if the username the user is entering already exists.

    static Future<bool> doesNameAlreadyExist(String value, String 
    name) async{
    final QuerySnapshot result = await Firestore.instance
      .collection('users')
      .where(value, isEqualTo: name)
      .limit(1)
      .getDocuments();
      final List<DocumentSnapshot> documents = result.documents;
     return  documents.length == 1;
    

    }

    When i call that method here i get this error enter image description here

    Is there a way to get a return type of bool from a Future