theSimm
theSimm

Reputation: 450

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

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

Upvotes: 2

Views: 12358

Answers (1)

vrachlin
vrachlin

Reputation: 817

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

Upvotes: 7

Related Questions