从未来的颤振中获取对象数据

时间:2021-02-06 11:14:32

标签: flutter future

我必须使用其他运行良好的 API 检查电子邮件和密码。问题是我的未来将返回一个具有令牌的类对象。我需要它用于其他屏幕,并在登录后导航到其他屏幕。

   Future<LoginResponse> createLoginState(String email, String password) async {
  final http.Response response = await http.post(
      'https://www.polestarkw.com/api/login',
      headers: <String, String>{
        'Accept': 'application/json',
        //'content-type' : 'application/json'
      },
      body: {
      "email":email ,
      "password":password ,
      });

  if (response.statusCode == 200) {
   // print(response.body);
    LoginResponse loginResponse=LoginResponse.fromJson(json.decode(response.body)) ;
    return loginResponse;
  } else {
    throw Exception('Failed to create album.');
  }
}

class LoginResponse {
  Object _data;
  String token_type;
  String expires_in;
  String access_token;
  String refresh_token;

  LoginResponse(
      {this.token_type, this.expires_in, this.access_token, this.refresh_token});

  LoginResponse.fromJson(Map<String, dynamic> json) {
    token_type = json['token_type'];
    expires_in = json['expires_in'];
    access_token = json['access_token'];
    refresh_token = json['refresh_token'];
  }


}

我的另一个页面上需要这个 loginResponse 对象。这里使用的是未来的实例。

 _futureJwt = createLoginState(emailController.text, pwdController.text);

如何从_futureJwt获取数据。

2 个答案:

答案 0 :(得分:0)

代码应该是这样的

   Future<LoginResponse> createLoginState(String email, String password) async {
  final http.Response response = await http.post(
      'https://www.polestarkw.com/api/login',
      headers: <String, String>{
        'Accept': 'application/json',
        //'content-type' : 'application/json'
      },
      body: {
      "email":email ,
      "password":password ,
      });

  if (response.statusCode == 200) {
   // print(response.body);
    LoginResponse loginResponse=fromJson(json.decode(response.body)) ;
    return loginResponse;
  } else {
    throw Exception('Failed to create album.');
  }
}
LoginResponse fromJson(Map<String, dynamic> json) {
    token_type = json['token_type'];
    expires_in = json['expires_in'];
    access_token = json['access_token'];
    refresh_token = json['refresh_token'];
    return LoginResponse(token_type,expires_in,access_token,refresh_token);
  }
class LoginResponse {
  Object _data;
  String token_type;
  String expires_in;
  String access_token;
  String refresh_token;

  LoginResponse(
      {this.token_type, this.expires_in, this.access_token, this.refresh_token});


}

上面的代码也应该按照你写的方式工作,但我不确定,因为我用这种方式
然后你可以像这样使用

LoginResponse _futureJwt = await createLoginState(emailController.text, pwdController.text);
var token_type = _futureJwt.token_type;
var expires_in = _futureJwt.expires_in;
var access_token = _futureJwt.access_token;
var refresh_token = _futureJwt.refresh_token;

就这么简单。如果你不想等待 Future,你可以像这样使用 .then

createLoginState(emailController.text, pwdController.text).then((_futureJwt){
   var token_type = _futureJwt.token_type;
   var expires_in = _futureJwt.expires_in;
   var access_token = _futureJwt.access_token;
   var refresh_token = _futureJwt.refresh_token;
});

答案 1 :(得分:0)

使用FutureBuilder

然后您可以使用 AsyncSnapshot 访问 hasData()hasError() 并像这样获取数据:

  @override
  Widget build(BuildContext context) {

    Future<String> exampleFuture = Future.delayed(Duration(seconds: 2), "value")

    return FutureBuilder(
      future: exampleFuture,
      builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
        if (snapshot.hasError) {
          return Text("error");
        } else if (!snapshot.hasData) {
          return Text("loading");
        } else {
          return Text(snapshot.data);
        }
    });
  }