flutter - 我怎样才能在飞镖中获得期货最终价值?
问题描述
我正在尝试这个,但它不起作用:
Future<bool> isGroupNameAvailable(String name) async {
bool result = true;
(await _fireStore.collection("groups").get()).docs.forEach((doc) {
if (name == doc.id) {
result = false;
}
});
return result;
}
FutureBuilder(
future: db!.isGroupNameAvailable(value),
builder: (BuildContext context,
AsyncSnapshot<bool> snap) {
setState(() {
_isGidUnique = snap.data!
? null
: "this group name is taken,try different one";
});
print("*************first" +
_isGidUnique.toString());
return Text("");
},
);
// db!
// .isGroupNameAvailable(value)
// .then((avilible) => setState(() {
// _isGidUnique = avilible
// ? null
// : "this group name is taken,try different one";
// }));
print(_isGidUnique);
return _isGidUnique;
_isGidUnique 应该是字符串“这个组名被采用,尝试不同的”但它变为空我认为未来的构建器由于某种原因无法工作:(
解决方案
您应该尝试使用 Future.wait 方法,如下所示:
/// Future.wait takes an array of futures and completes when they are all over
var docs = await Future.wait(_fireStore.collection("groups").get()).docs);
/// every returns true if all elements match the given predicate
return docs.every((doc) => name != doc.id);
推荐阅读
- python - Python integral calculator doesn't print a value
- python-3.x - 如何修复 java.net.SocketTimeoutException 引起的 MongoTimeoutException:连接超时
- android - 显示 dialogAlert 后应用失去焦点
- scala - 蛇游戏scala中的苹果位置
- sql - 在 MS SQL 中验证数据正确性的最佳实践
- node.js - 使用 nodejs 服务器设置 Vue.js
- javascript - 按特定顺序对 JS 数值数组进行排序
- laravel - Laravel 8 基于类的模型工厂
- python - 如何使用 docker 高效地输入文件
- python - 谁能告诉我如何用相同的按钮解决 python gui tkinter 但在不同的 MySQL 表中单独工作