Search code examples
flutterdartjson-serialization

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


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?


Solution

  • 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.