flutter freezed :Could not generate `toJson` code for `data` because of type

1,952

ResourceDataModel is a @freezed-annotated class, so it needs to mix in _$ResourceDataModel.

@freezed
abstract class ResourceDataModel with _$ResourceDataModel {
  // ...
}

Without that mixin, ResourceDataModel does not contain a toJson() method, and thus LanguageResourceResponseModel.toJson() cannot serialize a List<ResourceDataModel>.

Share:
1,952
Admin
Author by

Admin

Updated on December 06, 2022

Comments

  • Admin
    Admin over 1 year

    I am trying to create my model by freezed and json_serializable :

    import 'package:freezed_annotation/freezed_annotation.dart';
    
    part 'language_response_model.freezed.dart';
    part 'language_response_model.g.dart';
    
    @freezed
    abstract class LanguageResourceResponseModel
        with _$LanguageResourceResponseModel {
      const factory LanguageResourceResponseModel({
        required String id,
        required List<ResourceDataModel> data,
      }) = _LanguageResourceResponseModel;
    
      factory LanguageResourceResponseModel.fromJson(Map<String, dynamic> json) =>
          _$LanguageResourceResponseModelFromJson(json);
    }
    
    @freezed
    abstract class ResourceDataModel {
      const factory ResourceDataModel({
        required String key,
        required String value,
      }) = _ResourceDataModel;
    
      factory ResourceDataModel.fromJson(Map<String, dynamic> json) =>
          _$ResourceDataModelFromJson(json);
    }
    

    but I got this error :

    Could not generate `toJson` code for `data` because of type `ResourceDataModel`.
    package:test/splash/data/models/language_response_model.freezed.dart:144:33
        ╷
    144 │   final List<ResourceDataModel> data;
        │                                 ^^^^
        ╵
    [INFO] Running build completed, took 2.5s
    
    [INFO] Caching finalized dependency graph...
    [INFO] Caching finalized dependency graph completed, took 189ms
    
    [SEVERE] Failed after 2.7s
    

    What is the problem?

    dependencies:
      json_annotation: ^4.3.0
      json_serializable: ^6.0.1
    dev_dependencies:
      build_runner: ^2.1.5
      flutter_launcher_icons: ^0.9.1
      freezed: ^0.15.1+1
      freezed_annotation: ^0.15.0
    
    • MD Ismail Alam Khan
      MD Ismail Alam Khan over 2 years
      You either need to provide a toJson and fromJson with the JsonKey annotation or implement a JsonConverter class. pub.dev/packages/… showed here too just scroll a bit down
    • Cyrus the Great
      Cyrus the Great over 2 years
      By freezed you just need to implement FromJson , Where is to json ?@MDIsmailAlamKhan
    • Cyrus the Great
      Cyrus the Great over 2 years
      With these changes, Freezed will automatically ask json_serializable to generate all the necessary fromJson/toJson.
    • Nitrodon
      Nitrodon over 2 years
      ResourceDataModel is annotated with @freezed but doesn't mix in _$ResourceDataModel.
    • Cyrus the Great
      Cyrus the Great over 2 years
      Ops, you are right. It was my mistake . Please write answer I check you