Flutter从Future(布尔)返回bool类型。方法

编程入门 行业动态 更新时间:2024-10-10 18:28:16
本文介绍了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类型。方法

本文发布于:2023-11-25 21:20:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔   类型   方法   Flutter   Future

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!