【发布时间】:2021-02-06 11:14:32
【问题描述】:
我必须使用其他运行良好的 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 获取数据。
【问题讨论】: