颤振如何像变量一样使用Future返回值 [英] Flutter how to use Future return value as if variable
本文介绍了颤振如何像变量一样使用Future返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取 Future
返回值,并将其用作变量。
我有这个 Future
函数
I want to get Future
return value and use it like variable.
I have this Future
function
Future<User> _fetchUserInfo(String id) async {
User fetchedUser;
await Firestore.instance
.collection('user')
.document(id)
.get()
.then((snapshot) {
final User user = User(snapshot);
fetchedUser = user;
});
return fetchedUser;
}
我想得到这样的价值
final user = _fetchUserInfo(id);
但是当我尝试使用这种方式
However when I tried to use like this
new Text(user.userName);
Dart无法识别为 User
类。它说动态
。
如何获得返回值并使用它?
首先我做错了吗?
任何帮助表示赞赏!
Dart doesn't recognize as User
class. It says dynamic
.
How Can I get return value and use it?
Am I doing wrong way first of all?
Any help is appreciated!
推荐答案
您可以简化代码:
Future<User> _fetchUserInfo(String id) async {
User fetchedUser;
var snapshot = await Firestore.instance
.collection('user')
.document(id)
.get();
return User(snapshot);
}
您还需要async / await来获取值
you also need async/await to get the value
void foo() async {
final user = await _fetchUserInfo(id);
}
这篇关于颤振如何像变量一样使用Future返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文