copyWith method

  1. @override
JUndoHistoryState copyWith({
  1. bool? enabled,
  2. int? undoCount,
  3. int? redoCount,
})
override

Creates a copy of the current state with optional modifications.

This method should be implemented by subclasses to create a new instance of the state, allowing partial updates to specific properties without changing the original instance. For nullable fields, distinguish an omitted argument from an explicit null. One option is a private sentinel, with argument validation in the subclass:

const _unchanged = Object();

class ProfileState extends JState {
  final String? name;
  const ProfileState({this.name});

  @override
  ProfileState copyWith({Object? name = _unchanged}) {
    if (!identical(name, _unchanged) && name != null && name is! String) {
      throw ArgumentError.value(name, 'name', 'Must be a String or null');
    }
    return ProfileState(
      name: identical(name, _unchanged) ? this.name : name as String?,
    );
  }

  @override
  List<Object?> get props => [name];
}

Implementation

@override
JUndoHistoryState copyWith({bool? enabled, int? undoCount, int? redoCount}) =>
    JUndoHistoryState(
      enabled: enabled ?? this.enabled,
      undoCount: undoCount ?? this.undoCount,
      redoCount: redoCount ?? this.redoCount,
    );