具有enum属性的冻结类在尝试序列化时抛出错误



我有一个冻结的类,在它的构造函数中接受一个enum,但是当试图在这个类上执行jsonEncode方法时,它失败了,出现以下错误:

在处理手势时抛出以下JsonUnsupportedObjectError:将对象转换为可编码对象失败:Instance of 'InputType'

我已经用JsonValue("…")注释了我的枚举案例,但我没有看到任何为枚举生成的代码。

这是一个bug还是我做错了什么?

完整示例如下:

@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.

尝试使用@JsonKey(name: 'vote_count')代替

从您的代码中,我发现您省略了道具currentInputType的类型,因此将InputType附加到它后面,如下所示:

@Default(InputType.Bolus) InputType? currentInputType,

最新更新