25

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.

2
  • can you upload your JSON response here? Commented Mar 12, 2020 at 6:31
  • I didn't get it. The POJO in Java, isn't it PODO in dart? This is what i read everywhere. Commented Oct 22, 2020 at 5:00

3 Answers 3

20

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'])),

Sign up to request clarification or add additional context in comments.

3 Comments

how to know the line of error ? because a lot of json to dart ? have 400 lines with many children sub children json . how to debug thats mean to need added json.decode ? because json.decode is no method , i use jsonDecode() instead . but not work.
you should change "json" parameter name, cos it makes a problem while using "json.decode."
@YogiArifWidodo try to use jsonDecode(source) instead
2

Well, I solved this issue by decoding whole 'response' before using .fromJson

See the screesshot

Comments

0

In this case, you have to check the API response also. If it's returning a string convert to array.

return response()->json('Updated sucessfully !', 200);

Replace with

return response()->json(['message' => 'Updated sucessfully !', 200);


    

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.