6

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.

Update: freezed needs the enum type specified in the factory constructor! Default value is not enough.
Luca Cras
  • 61
  • 1
  • 3

2 Answers2

3

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

0

From your code, I spotted that you omitted the type of the prop currentInputType, so append InputType to it as in below:

@Default(InputType.Bolus) InputType? currentInputType,
its-me-mahmud
  • 690
  • 1
  • 7
  • 14