11

Consider the following json:

[
 {
  "id": 1
  "name": "foo"
 },
 {
  "id": 1
  "name": "foo"
 }
]

I am trying to parse this using json_serializable library.

json.decode(response.body) returns List<dynamic>.

but the json_serializable auto-generates methods fromJson and toJson with type Map<String, dynamic json>.

is there any way in json_serializable to parse List<dynamic> with auto-generated methods and not manually?

Akshar Patel
  • 8,998
  • 6
  • 35
  • 50

1 Answers1

3

you should try it this way;

@JsonSerializable()
class Example{
    int id; String name;
    Example({this.id, this.name})
}

call your Future function and save it in a variable {parsed} and then convert your mapped data to list by doing this:

List<Example> result = parsed.map((i) => Example.fromJson(i)).toList();