Exception: type 'String' is not a subtype of type 'Map<String, dynamic>'
{"collection":{"data":"{\"id\": 1, \"name\": \"Marko\", \"picture\":
\"https://lh3.googleusercontent.com/a-/AAuE7mC1vqaKk_Eylt-fcKgJxuN96yQ7dsd2dBdsdsViK959TKsHQ=s96-
c\"}","statusCode":202,"version":"1.0"}}
This is the above json and i want to put it at User pojo class only the [data].
But it threw the above exception type.
class UserCollection {
final User data;
final int statusCode;
final String version;
UserCollection({this.data, this.statusCode, this.version});
factory UserCollection.fromJson(Map<String, dynamic> json) {
return UserCollection(
statusCode: json['statusCode'] as int,
data: User.fromJson(json['data']) ,
version: json['version'] as String );
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['data'] = this.data;
data['statusCode'] = this.statusCode;
data['version'] = this.version;
return data;
}
}
User Pojo class
@JsonSerializable()
class User {
final int id;
final String sub;
final String home;
final String work;
final String name;
final String mobileNo;
final String email;
final String favMechId;
final String appVersionCode;
final String picture;
final String serverTime;
final String dateCreated;
final String dateModified;
final String fcmTokenId;
User(
{this.id,
this.sub,
this.home,
this.work,
this.name,
this.mobileNo,
this.email,
this.favMechId,
this.appVersionCode,
this.picture,
this.serverTime,
this.dateCreated,
this.dateModified,
this.fcmTokenId});
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String,dynamic> toJson() => _$UserToJson(this);
I have referring this medium site for clarity, medium flutter json
but in vein more than 4 hours i couldn't what was wrong. If change the User.from() to String then it's okay. But i need to parse the [data] from json to User pojo class.
Try below,
factory UserCollection.fromJson(Map<String, dynamic> json) {
return UserCollection(
statusCode: json['statusCode'] as int,
data: User.fromJson(json.decode(json['data'])),
version: json['version'] as String );
}
Change in data: User.fromJson(json.decode(json['data'])),
Well, I solved this issue by decoding whole 'response' before using .fromJson
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With