跳到主要内容

avoid_redundant_argument_values

稳定
修复可用

避免冗余的参数值。

详情

#

不要传递与对应参数的默认值匹配的参数。

请注意,方法覆盖可以更改参数的默认值,因此参数可能与一个默认值相等,但不与另一个相等。例如,有两个类 AB,其中 BA 的子类,并且 B 覆盖了在 A 中声明的方法,该方法在 A 的声明中有一个默认值,而在 B 的声明中有另一个不同的默认值。如果调用方法的目标的静态类型是 B,并且 B 的默认值与参数匹配,则可以省略该参数(如果参数值不同,则不会报告 lint)。但是,如果调用方法的目标的静态类型是 A,则可能会报告 lint,但我们无法静态地知道调用了哪个方法,因此报告的 lint 可能是误报。可以在行内使用注释 // ignore: avoid_redundant_argument_values 来忽略此类情况。

糟糕

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

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

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

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

启用

#

要启用 avoid_redundant_argument_values 规则,请在您的 analysis_options.yaml 文件中,在 linter > rules 下添加 avoid_redundant_argument_values

analysis_options.yaml
yaml
linter:
  rules:
    - avoid_redundant_argument_values

如果您正在使用 YAML 映射语法配置 linter 规则,请在 linter > rules 下添加 avoid_redundant_argument_values: true

analysis_options.yaml
yaml
linter:
  rules:
    avoid_redundant_argument_values: true