Search code examples
flutterjson-deserializationfreezed

Context: Field isn't final, but constructor is 'const'


I want to create quiz class with mutable variable.

So I use @unfreezed and this class also have json serializable.

so I have an error: Context: Field isn't final, but constructor is 'const'.

Because @unfreezed gen non final field, so auto generate file gen const constructor.

=> error

How to fix it?

Thank so much.

This is my class

@unfreezed
class Quiz with _$Quiz {
  const factory Quiz({
    @JsonKey(name: 'title') @Default(null) String? title,
    @JsonKey(name: 'categories') @Default(null) String? categories,
    @JsonKey(name: 'questions') @Default([]) List<Question> questions,
  }) = _Quiz;

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

Solution

  • To fix the error, you can remove the const keyword from the constructor.