0

first all sorry about my "engrish"... I'm trying to consume an own API method with http package in Flutter. I'm able to get a response from API but I'm having trouble trying to map the response (json) to my custom object called APILoginResponse.

I'm calling the API method like this:

APILoginResponse apiLogin = await api.apiLogin();

but I'm getting a runtime error "dynamic is not subtype of AccessToken".

Here is my API login method:

Future<APILoginResponse> apiLogin() async {
  final http.Response response = await http.post(
    api_end_point + '/api/Auth/login',
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(
        <String, String>{'userName': api_user, 'password': api_password}),
  );
  if (response.statusCode == 200) {
    return APILoginResponse.fromJson(json.decode(response.body));
  } else {
    throw Exception('Error en login de la API');
  }
}

...and here is my APILoginResponse object:

class APILoginResponse {
  final AccessToken accessToken;
  final String refreshToken;

  APILoginResponse({this.accessToken, this.refreshToken});

  factory APILoginResponse.fromJson(Map<String, dynamic> json) {
    return APILoginResponse(
      accessToken: json['accessToken'],
      refreshToken: json['refreshToken'],
    );
  }
}

class AccessToken {
  String token;
  int expiresIn;
}

error is in the line:

accessToken: json['accessToken']

inside APILoginResponse class.

Here is my json response:

{
  "accessToken": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiIiLCJzdWIiOiJib3N0b25jcmVkX2NsaWVudGVzIiwianRpIjoiZjBkMzY0ZDMtMmRkNS00NzkzLWE5ZTktMzY1YzJmODNiYmI3IiwiaWF0IjoxNTk0MTMxODAwLCJyb2wiOiJhcGlfYWNjZXNzIiwiaWQiOiIyMzg3YTMzZi1hYzE5LTRhMzYtODcyZC04MTE3MzExZDFjY2IiLCJuYmYiOjE1OTQxMzE3OTksImV4cCI6MTU5NDEzMjM5OSwiaXNzIjoid2ViQXBpIiwiYXVkIjoiaHR0cHM6Ly9sb2NhbGhvc3Q6NDQzMTcvLyJ9.PqCPkVct4e4duWFEr63fALZ0h_0x25vsV_GBx336Apw",
    "expiresIn": 600
  },
  "refreshToken": "W6wyiw9xYuC2UaJmyCOYujKIZTs0jAscnfcWTrEyVIk="
}

Any help with this will be appreciated. Thanks!

2 Answers 2

1

If you are sure that the returning value is an AccessToken you can try this:

  factory APILoginResponse.fromJson(Map<String, dynamic> json) {
   return APILoginResponse(
   accessToken: (json['accessToken'] as Map<String,dynamic>) as AccessToken ?? null,
   refreshToken: json['refreshToken'],
   );
 }

Change your AccessToken class to this:

class AccessToken {
   final Map<String,dynamic> tokenData;
   AccessToken(tokenData)
}
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks P4yam, unfortunately I get the same error with your code.
is that the same error? what does print(${json['accessToken']}) show?
{{token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiIiLCJzdWIiOiJib3N0b25jcmVkX2NsaWVudGVzIiwianRpIjoiZjQxNWJlMWEtNTgyZC00ZGY3LTgzOGItNDljYTdkZGJiNGNiIiwiaWF0IjoxNTk0MTQ1MTYwLCJyb2wiOiJhcGlfYWNjZXNzIiwiaWQiOiIyMzg3YTMzZi1hYzE5LTRhMzYtODcyZC04MTE3MzExZDFjY2IiLCJuYmYiOjE1OTQxNDUxNjAsImV4cCI6MTU5NDE0NTc2MCwiaXNzIjoid2ViQXBpIiwiYXVkIjoiaHR0cHM6Ly9sb2NhbGhvc3Q6NDQzMTcvLyJ9.bQLG-31ggBBsrz-7T6n5cIlwiBpaSoHrdfxla4-VaxQ, expiresIn: 600}}
I edited my original original post and added the json response from API
the type of accessToken is Map<String,dynamic>. I edit my answer. see if that works
|
0

well I think @P4yam answer was right but I was getting the same error over and over, so I changed my APILoginResponse class as follow:

class APILoginResponse {
  AccessToken accessToken;
  String refreshToken;

  APILoginResponse({this.accessToken, this.refreshToken});

  APILoginResponse.fromJson(Map<String, dynamic> json) {
    accessToken = json['accessToken'] != null
        ? new AccessToken.fromJson(json['accessToken'])
        : null;
    refreshToken = json['refreshToken'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.accessToken != null) {
      data['accessToken'] = this.accessToken.toJson();
    }
    data['refreshToken'] = this.refreshToken;
    return data;
  }
}

class AccessToken {
  String token;
  int expiresIn;

  AccessToken({this.token, this.expiresIn});

  AccessToken.fromJson(Map<String, dynamic> json) {
    token = json['token'];
    expiresIn = json['expiresIn'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['token'] = this.token;
    data['expiresIn'] = this.expiresIn;
    return data;
  }
}

and everything works fine now! thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.