Flutter将Future <T>转换为List <T>

时间:2019-12-26 08:57:01

标签: android flutter dart flutter-layout dart-pub

我要转换以下

Future<UserDetl> f = AppDatabase().userById('55e62a90-273c-11ea-9441-f75d5cd0114b');

进入List<UserDetl> lstUser

class UserDetl {
  String userId;
  String userName;
  String userPlace;
  String userUrl;

  UserDetl(this.userId,this.userName, this.userPlace, this.userUrl);

}

我想从列表中获取用户名

String user = lstUser[0].userName;

3 个答案:

答案 0 :(得分:0)

尝试一下!

getUser() async {

UserDetl tmp = await  AppDatabase().userById('55e62a90-273c-11ea-9441-f75d5cd0114b');

if(tmp!=null)
 setState((){
    UserDetl u = v;
});
}

答案 1 :(得分:0)

您只需要用结果构建一个列表:

Future<UserDetl> f = AppDatabase().userById('55e62a90-273c-11ea-9441-f75d5cd0114b');
UserDetl user = await f;
List<UserDetl> lstUser = [user];

答案 2 :(得分:0)

Future<UserDetl> f = //get your Futuredata
UserDetl user = await f;
List<UserDetl> lstUser = [user];//add in list

//you also can get it from list like:

UserDetl user = lstUser[0];