Contents

avoid_redundant_argument_values

Contents

Avoid redundant argument values.

This rule is available as of Dart 2.8.

This rule has a quick fix available.

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:

dart
void f({bool valWithDefault = true, bool? val}) {
  ...
}

void main() {
  f(valWithDefault: true);
}

GOOD:

dart
void f({bool valWithDefault = true, bool? val}) {
  ...
}

void main() {
  f(valWithDefault: false);
  f();
}

Usage

#

To enable the avoid_redundant_argument_values rule, add avoid_redundant_argument_values under linter > rules in your analysis_options.yaml file:

analysis_options.yaml
yaml
linter:
  rules:
    - avoid_redundant_argument_values