type 'int' is not a subtype of type 'Map<String, dynamic>'

Viewed 31

How can I fix this issue?

Here is my model class

@JsonSerializable()
class FollowsModel {
  int userId;

  FollowsModel();

  factory FollowsModel.fromJson(Map<String, dynamic> map){
    return  _$FollowsModelFromJson(map);
  }
}

JSON serializable code

FollowsModel _$FollowsModelFromJson(Map<String, dynamic> json){
  return FollowsModel()
    ..userId =  json['userId'] as int;
}

This is the code that is bringing problems

          followsModel = list.isNotEmpty ? list.map<FollowsModel>((e) => FollowsModel.fromJson(e)).toList(): [];

Api response

I/flutter ( 3121): Response Text:
I/flutter ( 3121): {"message":"List of users followed by me.","data":[10393]}

1 Answers

Your api response is a list of int, but in parse method you expect Map<String, dynamic>, so just change those map to int. Like this:

factory FollowsModel.fromJson(int id){
    return  _$FollowsModelFromJson(id);
  }

FollowsModel _$FollowsModelFromJson(int id){
  return FollowsModel()
    ..userId =  id;
}
Related