我想从异步方法获取数据作为字符串,但是我获取了Future <dynamic>的实例

时间:2020-10-06 03:23:06

标签: flutter google-cloud-firestore

我尝试了这种逻辑。有了它,我可以打印所需的数据,但是不能将其保存到局部变量中。

{
   var temp;
      //gets the user details using firestore and uid
      final result = firestoreServices.getUserDetails(user.uid);
      result.then((data) {
        temp = data['role'];
      }, onError: (e) {
        print(e);
      });
      print("temp: --> $temp");
     
      return UserModel(uid: user.uid, role: userRole);
}
   

如果我打印温度,它将打印为空。

我不想使用将来的构建器。我有什么办法吗?

1 个答案:

答案 0 :(得分:0)

尝试一下

{
   var temp;
      //gets the user details using firestore and uid
      final result = await firestoreServices.getUserDetails(user.uid);
      temp = data['role'];
      print("temp: --> $temp");
      return UserModel(uid: user.uid, role: userRole);
}