Flutter从Future(布尔)返回bool类型。方法 [英] Flutter returning a bool type from a Future<bool> Method

查看:2158
本文介绍了Flutter从Future(布尔)返回bool类型。方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与



是否有一种方法可以从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)) == true

Read more about async programming here: Dart Futures

这篇关于Flutter从Future(布尔)返回bool类型。方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆