'Freezed class with enum property throws error upon trying to serialize

I have a freezed class that takes an enum in its constructor, but when trying to perform the jsonEncode method on this class, it fails with the following error:

The following JsonUnsupportedObjectError was thrown while handling a gesture: Converting object to an encodable object failed: Instance of 'InputType'

I have annotated my enum cases with JsonValue("...") but I do not see any generated code for the enum.

Is this a bug or am I doing something wrong?

Full example below:

@freezed
class Input with _$Input {
  const factory Input({
    @Default(0) int seconds,
    @Default(0) double bolus,
    @Default(0) double infusion,
    @Default(InputType.Bolus) currentInputType,
  }) = _Input;

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

enum InputType {
  @JsonValue("bolus")
  Bolus,
  @JsonValue("infusion")
  Infusion,
}

// When calling jsonEncode(someInput); throws the specified error.


Solution 1:[1]

Try to use @JsonKey(name: 'vote_count') instead

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yevhen Samoilov