在以下代码段中, state.copyWith 函数不可用。
@freezed
class MyState with _$MyState {
@JsonSerializable(fieldRename: FieldRename.snake, explicitToJson: true)
const factory MyState({@Default(0) int counter,}) = _MyState;
const factory MyState.initial({@Default(0) int counter}) = Initial;
const factory MyState.loading() = Loading;
const factory MyState.one() = One;
const factory MyState.two() = Two;
factory MyState.fromJson(Map<String, dynamic> json) =>
_$MyStateFromJson(json);
}
class MyStateNotifier extends StateNotifier<MyState> {
MyStateNotifier() : super(MyState.initial());
Future<void> one() async {
state = MyState.loading();
await Future.delayed(Duration(seconds: 5));
state.copyWith(counter: 1);
}
}
但是,当我删除密封类时,copyWith 函数可用。
@freezed
class MyState with _$MyState {
@JsonSerializable(fieldRename: FieldRename.snake, explicitToJson: true)
const factory MyState({@Default(0) int counter,}) = _MyState;
// const factory MyState.initial({@Default(0) int counter}) = Initial;
// const factory MyState.loading() = Loading;
// const factory MyState.one() = One;
// const factory MyState.two() = Two;
factory MyState.fromJson(Map<String, dynamic> json) =>
_$MyStateFromJson(json);
}
class MyStateNotifier extends StateNotifier<MyState> {
MyStateNotifier() : super(MyState());
Future<void> one() async {
await Future.delayed(Duration(seconds: 5));
state.copyWith(counter: 1);
}
}
我需要更改什么才能使第一个片段中的 copyWith 可用?