admin管理员组文章数量:1353814
Suppose this were my freezed class:
@freezed
class User with _$User {
factory User({
required String uuid,
required UniqueKey key,
@Default('') String name,
}) = _User;
factory User.initial() => User(
uuid: '',
key: UniqueKey(),
);
factory User.fromJson(Map<String, dynamic> json) =>
_$UserFromJson(json);
}
This throws an error Could not generate fromJson code for key.
So I tried
factory User.fromJson(Map<String, dynamic> json) =>
_$UserFromJson({...json, 'key': UniqueKey()});
but this is not valid either. It says "to support UniqueKey you can use JsonConverter"- I don't understand how to.
I also tried adding @JsonKey(includeFromJson: false, includeToJson: false)
but that throws another error.
Suppose this were my freezed class:
@freezed
class User with _$User {
factory User({
required String uuid,
required UniqueKey key,
@Default('') String name,
}) = _User;
factory User.initial() => User(
uuid: '',
key: UniqueKey(),
);
factory User.fromJson(Map<String, dynamic> json) =>
_$UserFromJson(json);
}
This throws an error Could not generate fromJson code for key.
So I tried
factory User.fromJson(Map<String, dynamic> json) =>
_$UserFromJson({...json, 'key': UniqueKey()});
but this is not valid either. It says "to support UniqueKey you can use JsonConverter"- I don't understand how to.
I also tried adding @JsonKey(includeFromJson: false, includeToJson: false)
but that throws another error.
1 Answer
Reset to default 1Option 1. Remove
factory User.fromJson(Map<String, dynamic> json) =>
_$UserFromJson(json);
from your freezed class, if you don't need the object to be serializable to json.
Option 2. If you do need it to be serializable to json then you can't use UniqueKey. You can instead generate a unique number and put that in the freezed class, and then use ValueKey(that number) to get a key.
本文标签: flutterInclude UniqueKey in freezed classStack Overflow
版权声明:本文标题:flutter - Include UniqueKey in freezed class - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743917346a2561449.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论