Dart:Convert nested object to Map with json_annotaion

Viewed 47
import 'package:json_annotation/json_annotation.dart';
part 'soul_chat_videoparty_avatar.g.dart';

@JsonSerializable()
class VideoPartyAvatarCategory {
  VideoPartyAvatarCategory();
  List<VideoPartyAvatar>? avatarResourceList;

  factory VideoPartyAvatarCategory.fromJson(Map<String, dynamic> json) =>
      _$VideoPartyAvatarCategoryFromJson(json);
  Map<String, dynamic> toJson() => _$VideoPartyAvatarCategoryToJson(this);
}

@JsonSerializable()
class VideoPartyAvatar {
  VideoPartyAvatar();

  factory VideoPartyAvatar.fromJson(Map<String, dynamic> json) =>
      _$VideoPartyAvatarFromJson(json);
  Map<String, dynamic> toJson() => _$VideoPartyAvatarToJson(this);
}


My data structure is like above.A category has a list with a specific type.So I use json_annotion to convert data after I fetch it from my server.After that I try to send it back to my server.So I use the method VideoPartyAvatarCategory.toJson to convert it to a map to send.Although the category is converted to a map,the list still contains this specific type,not a map.

It can't be a map apparently because the convert code is like this in .g.dart file.


Map<String, dynamic> _$VideoPartyAvatarCategoryToJson(
        VideoPartyAvatarCategory instance) =>
    <String, dynamic>{
      'avatarResourceList': instance.avatarResourceList,
    };

So is there a way to convert a nested object to a Map totally?I tried using decode or encode method,convert it to String then to a Map,it can approach,which isn't perfect.

0 Answers
Related