0

Error while parsing JSON flutter

I/flutter ( 6858): type 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'List<SearchModel>'

Where am I doing wrong? How could I parse exact file /line-location of error?

{
  "total": 24,
  "totalPages": 1,
  "currentPage": 1,
  "perPageRecords": 50,
  "values": [
    {
      "_id": "563e2cce31821525104dfe0c",
      "name": "dard ho dil mein to dawaa kiji",
      "deleted": false
    },
    {
      "_id": "563e2cce31821525104dfe0b",
      "name": "mulk-e-adam main yaaruun ky Id ho rahi hai",
      "description": "",
      "deleted": false
    }
]

Sample Model crearted this way

class SearchPagination {
  int total;
  int totalPages;
  String currentPage;
  int perPageRecords;
  List<SearchModel> values;

  SearchPagination({this.total, this.totalPages, this.currentPage, this.perPageRecords, this.values});

  factory SearchPagination.fromJson(Map<String, dynamic> json) {
    return SearchPagination(
      total: json["total"] as int,
      totalPages: json["totalPages"] as int,
      currentPage: json["currentPage"] as int,
      perPageRecords: json["perPageRecords"] as int,
      values: json["values"].map((a) => SearchModel.fromJson(a)),
    );
  }
}

class SearchModel {
  final String id;
  final String name;
  final String description;
  final bool deleted;

  SearchModel({this.id, this.name, this.description, this.deleted});

  factory SearchModel.fromJson(Map<SearchModel, dynamic> json) {
    return SearchModel(id: json["id"], name: json["name"], description: json["description"] as String, deleted: json["deleted"]);
  }
}

This way service is being called

  Future<SearchPagination> fetchSearches({int page: 1}) async {
    var url = Uri.https(baseFinalUrl, 'additional/searches', {'page': page.toString()});
    return _getJson(url).then((json) {
      print(json);

      return SearchPagination.fromJson(json);
    });
  }

What is exact error while parsing JSON? Is there any line to line debugger for a flutter?

2 Answers 2

3

create your dart class by visiting here

paste your json data over there it will output the required class file

use that class file to fetch your json data

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

Comments

1

Replace

      values: json["values"].map((a) => SearchModel.fromJson(a)),

with

      values: (json["values"] as List).map((a) => SearchModel.fromJson(a)).toList(),

Because the result of map isn't a list, it has to be converted to List.

4 Comments

this ended with I/flutter ( 7144): type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<SearchModel, dynamic>'
Why is SearchModel.fromJson taking Map<SearchModel, dynamic> as argument, shouldn't it be SearchModel.fromJson(Map<String, dynamic> json)?
Corrected, Fixed. Thanks
BTW, Its better to use json_serializable and json_annotation packages, to auto generate these methods. Worth a look

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.