1

Im working with Freezed in flutter/dart and I want to generete a fromJson/toJson in a freezed class that has an abstract class as a parameter, but I get an error with the abstract class.

Here is the abstract class:

abstract class IModel{
   String getModelId();
}

And the freezed class that Implements it:

@freezed
class Model with _$Model implements IModel {
  const Model._();
  const factory Model({
    required String id
  }) = _Model;
  factory Model.fromJson(Map<String, Object?> json) => _$ModelFromJson(json);

  @override
  String getModelId() {
    return id;
  }
}

Now, my issue comes when I have another class with freezed, that has a member of the IModel type like so:

@freezed
class ModelGroup with _$ModelGroup {
  const factory ModelGroup({
    required List<IModel> models
  }) = _ModelGroup;
  factory ModelGroup.fromJson(Map<String, Object?> json) => _$ModelGroupFromJson(json);
  //I get an error when creating the fromJson here
}

I get an error on the ModelGroup class that says that I can't create the fromJson/toJson due to IModel, and I cant figure it out, any ideas on what I might be doing wrong?

BrML
  • 35
  • 6

1 Answers1

0

Update: I recently found a solution to this issue, following this answer.

I made a custom toJson and fromJson like the "exercisesFromJson" and "exercisesToJson" and It worked like a charm!

BrML
  • 35
  • 6