Json_Annotation does not handle nested classes properly

I have nested classes InventoryResult and Item that I want to read/write with json files, and the generated code by json_annotation does not map Item list in the toJson() method; curiously handling is totally fine for fromJson() method.
Below are my 2 classes:

@JsonSerializable()
class InventoryResult {
  List<Item> results;
  int number;
  int offset;
  int totalResults;

  InventoryResult({required this.results, required this.number, required this.offset, required this.totalResults});
  factory InventoryResult.fromJson(Map<String, dynamic> json) => _$InventoryResultFromJson(json);
  Map<String, dynamic> toJson() => _$InventoryResultToJson(this);

}

@JsonSerializable()
class Item {
  int id;
  String productName;

  Item(
      {required this.id,
      required this.productName});

  factory Item.fromJson(Map<String, dynamic> json) => _$ItemFromJson(json);
  Map<String, dynamic> toJson() => _$ItemToJson(this);
}

and Inventory’s generated fromJson() and toJson() methods differ oddly:

InventoryResult _$InventoryResultFromJson(Map<String, dynamic> json) =>
    InventoryResult(
      results: (json['results'] as List<dynamic>)
          .map((e) => Item.fromJson(e as Map<String, dynamic>))
          .toList(),
      number: (json['number'] as num).toInt(),
      offset: (json['offset'] as num).toInt(),
      totalResults: (json['totalResults'] as num).toInt(),
    );

Map<String, dynamic> _$InventoryResultToJson(InventoryResult instance) =>
    <String, dynamic>{
      'results': instance.results, //INCORRECT NESTED CLASS HANDLING HERE
      'number': instance.number,
      'offset': instance.offset,
      'totalResults': instance.totalResults,
    };

Naturally as a result, when I read from Json everything is correct, when I write to json it just writes as ‘Instance’ instead of correctly parsing Item object. I have json_annotation and json_serializable also up to date and totally stumped. Can it be related to that I added Item’s toJson() method later in the project? I have re-built many times after that, also tried deleting generated g.dart files altogether as well. Both class files import each other and json_annotation package, and the g.dart files. Item’s generated g.dart file appears correct with fromJson() and toJson() methods. I cannot find the issue.

Add the explicitToJson: true flag to the @JsonSerializable annotation of the outer class InventoryResult:

@JsonSerializable(explicitToJson: true)
class InventoryResult {
  // Your existing class fields and methods
}

This ensures that the generated toJson method will explicitly call toJson on nested objects like Item.

Now, the generated _$InventoryResultToJson method will look like this:

Map<String, dynamic> _$InventoryResultToJson(InventoryResult instance) =>
    <String, dynamic>{
      'results': instance.results.map((e) => e.toJson()).toList(), // Correctly serializes each Item
      'number': instance.number,
      'offset': instance.offset,
      'totalResults': instance.totalResults,
    };

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật