1
  @freezed
  class ABCModel with _$ABCModel {
    factory ABCModel({
      @JsonKey(name: "id") @Default('') String id,
      @JsonKey(name: "name") @Default('') String name,
    }) = _ABCModel;

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


  @freezed
  class EFGModel with _$EFGModel {
    factory EFGModel({
      @JsonKey(name: "abc") @Default(ABCModel()) ABCModel abc, //empty ABCModel
    }) = _EFGModel;

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

如果 EFGModel 得到一个空的或 null abc json 值,什么是适合放在@Default()冻结注释上的值,@Default(ABCModel())是不正确的

4

1 回答 1

1

请注意下面的 Player.blank() 构造函数。像这样...

@freezed
class Player with _$Player {
  Player._();

  factory Player({
    @Default('') String handle,
    @Default('') String realname,
    Contact? contactinfo,
    @Default(false) bool emailverified,
    @Default(false) bool showemail,
    @Default(false) bool showphone,
    @Default('') String avatarurl,
    DateTime? datejoined,
    @Default(0) int transactions,
    DateTime? datelasttransaction,
    DateTime? datelastlogin,
    @Default([]) List<String> tags,
    @Default([]) List<String> leagues,
    @Default([]) List<String> events,
    @Default(0) int views,
    @Default(0) int likes,
    @Default(0) int loginfails,
    @JsonKey(ignore: true) @Default('') String password,
    @JsonKey(ignore: true) @Default('') String confirm,
    required Meta meta,
  }) = _Player;

  factory Player.blank() => Player(contactinfo: Contact(), meta: Meta());
  factory Player.fromJson(Map<String, dynamic> json) => _$PlayerFromJson(json);
于 2021-07-23T14:16:31.000 回答