1

有没有办法用冻结定义多态 copyWith 方法?我现在正在做这样的事情,但我不想重复自己......

abstract class Schedulable {
  String get title;
  DateTime get start;
  DateTime get end;
}

@freezed
abstract class Task with _$Task implements Schedulable {
  const factory Task(
      {String title,
       @required @TimestampConverter() DateTime start,
       @required @TimestampConverter() DateTime end,
       String taskOnlyField }) = _Task;
}

@freezed
abstract class TimeEntry with _$TimeEntry implements Schedulable {
  const factory TimeEntry(
      {String title,
       @required @TimestampConverter() DateTime start,
       @required @TimestampConverter() DateTime end,
       String timeEntryOnlyField }) = _TimeEntry;
}


if (schedule is Task) {
  schedule = (schedule as Task).copyWith(start: start);
} else if (schedule is TimeEntry) {
  schedule = (schedule as TimeEntry).copyWith(start: start);
}
4

0 回答 0