json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

Holding a list of interface type while implementing this interface in different classes

Open vicdotdevelop opened this issue 4 years ago • 5 comments

Hey guys,

I have the following use case and cannot resolve it. Maybe you got an idea whats wrong. In general this is a quiz, the quiz has different types of questions like multiple choice, simple input and so on. To get a list of Questions with these different types I have started to declare it as the following.

I can't generate successfully the fromJson method for IQuestion. But I dont get the problem as I just want to use it as Base to provide the properties to the different Question-Classes as well as holding all different question types in one List<IQuestion>.

Maybe you got an idea what I am doing wrong as I am new to the json_serializable annotation as well as freezed.

Errorcode:

Could not generate `fromJson` code for `questions` because of type `IQuestion`.
package:greenzorro/domain/models/carbon_questionnaire.freezed.dart:40:23
   ╷
40 │   List<IQuestion> get questions => throw _privateConstructorUsedError;
   │                       ^^^^^^^^^
   ╵

Class that holds the list of questions

@freezed
@JsonSerializable(explicitToJson: true)
class Questionnaire with _$Questionnaire {
  factory Questionnaire({
    required List<IQuestion> questions,
  }) = _Questionnaire;

  factory Questionnaire.fromJson(Map<String, dynamic> json) =>
      _$QuestionnaireFromJson(json);
}

Interface

abstract class IQuestion {
  IQuestion({this.question, this.type, this.category, this.subCategory});
  String? question;
  Type? type;
  Category? category;
  String? subCategory;
}

Implementations of Questions

There are some more but this should explain the problem.

@JsonSerializable(explicitToJson: true)
@freezed
class SimpleInputQuestion with _$SimpleInputQuestion {
  @Implements(IQuestion)
  factory SimpleInputQuestion({
    Category? category,
    String? question,
    String? subCategory,
    Type? type,
  }) = _SimpleInputQuestion;

  factory SimpleInputQuestion.fromJson(Map<String, dynamic> json) =>
      _$SimpleInputQuestionFromJson(json);
}

@JsonSerializable(explicitToJson: true)
@freezed
class SimpleMultipleChoiceQuestion with _$SimpleMultipleChoiceQuestion {
  @Implements(IQuestion)
  factory SimpleMultipleChoiceQuestion({
    Category? category,
    String? question,
    String? subCategory,
    Type? type,
    required List<String> answers,
  }) = _SimpleMultipleChoiceQuestion;

  factory SimpleMultipleChoiceQuestion.fromJson(Map<String, dynamic> json) =>
      _$SimpleMultipleChoiceQuestionFromJson(json);
}

vicdotdevelop avatar Oct 31 '21 10:10 vicdotdevelop

IQuestion needs a fromJson on it. That's it.

Subclasses are hard.

kevmoo avatar Oct 31 '21 21:10 kevmoo

IQuestion needs a fromJson on it. That's it.

Subclasses are hard.

seems like it did the magic trick to extend the class like this. thanks for the hint :) A toJson was missing as well.

@JsonSerializable(explicitToJson: true)
abstract class IQuestion {
  IQuestion({this.question, this.type, this.category, this.subCategory});
  String? question;
  Type? type;
  Category? category;
  String? subCategory;

  factory IQuestion.fromJson(Map<String, dynamic> json) =>
      _$IQuestionFromJson(json);

  Map<String, dynamic> toJson() => _$IQuestionToJson(this);
}

vicdotdevelop avatar Oct 31 '21 23:10 vicdotdevelop

@kevmoo when i try to run the code then it gives me the following error. When I see the documentation there is no specific call for like the base constructor. any suggestions?

The non-abstract class '_$_SimpleInputQuestion' is missing implementations for these members:
 - IQuestion.category
 - IQuestion.question
 - IQuestion.subCategory
 - IQuestion.type
Try to either
 - provide an implementation,
 - inherit an implementation from a superclass or mixin,
 - mark the class as abstract, or
 - provide a 'noSuchMethod' implementation.

vicdotdevelop avatar Oct 31 '21 23:10 vicdotdevelop

Are you using something besides json_serializable? pkg:freezed or similar?

On Sun, Oct 31, 2021 at 4:39 PM Victor Blaess @.***> wrote:

@kevmoo https://github.com/kevmoo when i try to run the code then it gives me the following error. When I see the documentation there is no specific call for like the base constructor. any suggestions?

The non-abstract class '_$_SimpleInputQuestion' is missing implementations for these members:

  • IQuestion.category
  • IQuestion.question
  • IQuestion.subCategory
  • IQuestion.type Try to either
  • provide an implementation,
  • inherit an implementation from a superclass or mixin,
  • mark the class as abstract, or
  • provide a 'noSuchMethod' implementation.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/google/json_serializable.dart/issues/1019#issuecomment-955813566, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAEFCRLTQC5SJZ3HUAOYK3UJXHTHANCNFSM5HCDZ56A .

kevmoo avatar Nov 01 '21 00:11 kevmoo

Are you using something besides json_serializable? pkg:freezed or similar? On Sun, Oct 31, 2021 at 4:39 PM Victor Blaess @.***> wrote: @kevmoo https://github.com/kevmoo when i try to run the code then it gives me the following error. When I see the documentation there is no specific call for like the base constructor. any suggestions? The non-abstract class '_$_SimpleInputQuestion' is missing implementations for these members: - IQuestion.category - IQuestion.question - IQuestion.subCategory - IQuestion.type Try to either - provide an implementation, - inherit an implementation from a superclass or mixin, - mark the class as abstract, or - provide a 'noSuchMethod' implementation. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#1019 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAEFCRLTQC5SJZ3HUAOYK3UJXHTHANCNFSM5HCDZ56A .

Yes I am using freezed as well. You can see it in the implemented code in my question above. Any idea how I can solve this? I have actually deleted the json_serializable annotation for the classes where I have added @freezed as it seems like freezed provides the generation of fromJson and toJson as well. I have only added the JsonSerializable class to my interface.

vicdotdevelop avatar Nov 01 '21 08:11 vicdotdevelop