Skip to main content

add-copy-with

added in: 1.2.0
⚙️
Pro+

Warns when a class that matches the config does not declare a copyWith method.

note

This rule requires configuration in order to highlight any issues.

⚙️ Config

Set class-name-pattern (default is none) to configure the list of class names that need to declare a copyWith method.

Set file-name-pattern (default is none) to configure the list of file names with classes that need to declare a copyWith method.

analysis_options.yaml
dart_code_metrics:
rules:
- add-copy-with:
class-name-pattern: State$
file-name-pattern: '.model.dart'

Example

❌ Bad:

// LINT: Add a 'copyWith' method to classes that match 'State$'.
class SomeState {
final String value;

const SomeState({required this.value});
}

✅ Good:

class SomeState {
final String value;

const SomeState({required this.value});

SomeState copyWith({String value}) => SomeState(value: value ?? this.value);
}