0
votes

I have to check the email and password with the rest API that is going well. The problem is my future is returning a class object that has a token. I need that that for other screen and after login navigate to other screens.

   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'];
  }


}

I need this loginResponse object on my other page. Here is using a future instance.

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

how to get data from _futureJwt.

2

2 Answers

0
votes

The code should go something like this

   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});


}

The above code should work in the way u have written it too but I am not sure since I use this way
Then you can use this like

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;

As simple as that. If you do not want to wait for the Future, you can use .then like this

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;
});
0
votes

Use FutureBuilder.

Then you can use AsyncSnapshot to access hasData(), hasError() and get the data like so:

  @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);
        }
    });
  }