这个问题与
是否有一种方法可以从Future中获取布尔类型的返回类型
解决方案从 doesNameAlreadyExist 返回的返回类型为 Future< bool> ; ,所以行 doesNameAlreadyExist( userName,usernameController.value)== true ,实际上是 Future< bool> == bool 。 您需要等待,然后再等待结果。
doesNameAlreadyExist( userName,usernameController.value)。然后(值=>值== true)或
(等待dosNameAlreadyExist( userName,usernameController.value))==真在此处详细了解异步编程: Dart期货
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
Is there a way to get a return type of bool from a Future
解决方案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)) == trueRead more about async programming here: Dart Futures
更多推荐
Flutter从Future(布尔)返回bool类型。方法
发布评论