如何将 Future Map 分配给 Flutter 中的静态变量?
How to assign a Future Map to a static variable in Flutter?
我想收集所有数据并将它们放入一个映射变量中:
//function
Future<Map<String, String>?> getLoginUser() async{
Map<String,String> ?userLoginInfo;
userTable!.get().then((res) {
var data = res.data;
userLoginInfo!.update(data['userEmail'],data['userPwd']);
});
return userLoginInfo;
}
//I want to use the above function
Map<String,String>mockUsers=.getLoginUser() as Map<String, String>;
然而运行只是浪费时间,谁能告诉我如何使用这个未来的?
谢谢!
使用 async
关键字 removes/simplifies then()
的用法
Future<Map<String, String>?> getLoginUser() async{
Map<String,String>? userLoginInfo;
var data = (await userTable!.get()).data;
userLoginInfo!.update(data['userEmail'],data['userPwd']);
return userLoginInfo;
}
用法
Map<String,String> mockUsers = await getLoginUser()!;
或
Map<String,String>? mockUsers = await getLoginUser();
我想收集所有数据并将它们放入一个映射变量中:
//function
Future<Map<String, String>?> getLoginUser() async{
Map<String,String> ?userLoginInfo;
userTable!.get().then((res) {
var data = res.data;
userLoginInfo!.update(data['userEmail'],data['userPwd']);
});
return userLoginInfo;
}
//I want to use the above function
Map<String,String>mockUsers=.getLoginUser() as Map<String, String>;
然而运行只是浪费时间,谁能告诉我如何使用这个未来的?
谢谢!
使用 async
关键字 removes/simplifies then()
Future<Map<String, String>?> getLoginUser() async{
Map<String,String>? userLoginInfo;
var data = (await userTable!.get()).data;
userLoginInfo!.update(data['userEmail'],data['userPwd']);
return userLoginInfo;
}
用法
Map<String,String> mockUsers = await getLoginUser()!;
或
Map<String,String>? mockUsers = await getLoginUser();