copyWith abstract method

JState copyWith()

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

JState copyWith();