0

Will @JsonSerialization take care of the List mapping if there exists a List in Flutter?

@JsonSerializable()
class Content {
  Content(this.contentItems);

  @JsonKey(name: "content-items")
  List<Movie> contentItems;

  factory Content.fromJson(Map<String, dynamic> json) =>
      _$ContentFromJson(json);

  Map<String, dynamic> toJson() => _$ContentToJson(this);
}

In the above class do we need to manually convert the json to list of Movie?

MendelG
  • 14,885
  • 4
  • 25
  • 52
Abraham Mathew
  • 2,029
  • 3
  • 21
  • 42

1 Answers1

0

All the required methods and mapping will be generated by @JsonSerializable annotation. We will have to provide the _$ContentFromJson(json) and _$ContentToJson(this) as given in the question.

For me this was not working initially.. After generating the g.dart files also it was not behaving properly.Then after restarting the machine I was able to get the expected result. Don't know what was causing the error yet.

Abraham Mathew
  • 2,029
  • 3
  • 21
  • 42