avoid_ redundant_ argument_ values
Avoid redundant argument values.
Details
#DON'T pass an argument that matches the corresponding parameter's default value.
Note that a method override can change the default value of a parameter, so that
an argument may be equal to one default value, and not the other. Take, for
example, two classes, A and B where B is a subclass of
A, and B
overrides a method declared on A, and that method has a parameter with one
default value in A's declaration, and a different default value in B's
declaration. If the static type of the target of the invoked method is B, and
B's default value matches the argument, then the argument can be omitted (and
if the argument value is different, then a lint is not reported). If, however,
the static type of the target of the invoked method is A, then a lint may be
reported, but we cannot know statically which method is invoked, so the reported
lint may be a false positive. Such cases can be ignored inline with a comment
like // ignore: avoid_redundant_argument_values.
BAD:
void f({bool valWithDefault = true, bool? val}) {
...
}
void main() {
f(valWithDefault: true);
}
GOOD:
void f({bool valWithDefault = true, bool? val}) {
...
}
void main() {
f(valWithDefault: false);
f();
}
Enable
#
To enable the avoid_redundant_argument_values rule, add avoid_redundant_argument_values
under
linter > rules in your analysis_options.yaml
file:
linter:
rules:
- avoid_redundant_argument_values
If you're instead using the YAML map syntax to configure linter rules,
add avoid_redundant_argument_values: true under linter > rules:
linter:
rules:
avoid_redundant_argument_values: true
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Report an issue.