Skip to content

avoid_incomplete_copy_with

v0.4.0 Warning Collection & Type

A copyWith method that does not include all parameters from the class’s default constructor is incomplete. Callers cannot override every field, which defeats the purpose of having a copyWith method in the first place.

An incomplete copyWith is a common source of subtle bugs. When a new field is added to a class but not to copyWith, callers silently lose the ability to override that field. This rule ensures your copyWith stays in sync with the constructor.

See also: Effective Dart: Design

// copyWith is missing the 'surname' parameter
class IncompletePerson {
const IncompletePerson({required this.name, required this.surname});
final String name;
final String surname;
IncompletePerson copyWith({String? name}) {
return IncompletePerson(name: name ?? this.name, surname: surname);
}
}
// copyWith is missing both 'port' and 'path'
class IncompleteConfig {
const IncompleteConfig({
required this.host,
required this.port,
required this.path,
});
final String host;
final int port;
final String path;
IncompleteConfig copyWith({String? host}) {
return IncompleteConfig(host: host ?? this.host, port: port, path: path);
}
}
// copyWith includes all constructor parameters
class CompletePerson {
const CompletePerson({required this.name, required this.surname});
final String name;
final String surname;
CompletePerson copyWith({String? name, String? surname}) {
return CompletePerson(
name: name ?? this.name,
surname: surname ?? this.surname,
);
}
}
// No copyWith method — no warning
class NoCopyWith {
const NoCopyWith({required this.value});
final int value;
}

To disable this rule:

plugins:
many_lints:
diagnostics:
avoid_incomplete_copy_with: false