avoid-passing-default-values
Warns when an invocation has an argument that matches the parameter's default value.
warning
If you are depending on external API, you might want to keep explicitly passed default values in case the API changes. Consider this before enabling this rule.
⚙️ Config
Set ignored-parameters
(default is empty) to ignore specific named parameters (example).
Set ignored-values
(default is empty) to ignore specific default values (example).
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-passing-default-values:
ignored-parameters:
- someName
ignored-values:
- false
- MyEnum.someValue
- SomeClass.staticVariable
Example
❌ Bad:
class WithString {
final String value;
const WithString({this.value = 'some'});
}
void main() {
WithString(value: 'some'); // LINT: Avoid explicitly passing values equal to the parameter's default value. Try removing the passed argument.
}
✅ Good:
class WithString {
final String value;
const WithString({this.value = 'some'});
}
void main() {
WithString(value: 'another'); // Correct, different value
}
Example with "ignored-parameters"
Config
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-passing-default-values:
ignored-parameters:
- value
✅ Good:
class WithString {
final String value;
const WithString({this.value = 'some'});
}
void main() {
WithString(value: 'some'); // Correct, 'value' is ignored
}
Example with "ignored-values"
Config
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-passing-default-values:
ignored-values:
- 'some'
✅ Good:
class WithString {
final String value;
const WithString({this.value = 'some'});
}
void main() {
WithString(value: 'some'); // Correct, 'some' is ignored
}