跳到主要内容

matching_super_parameters

稳定版

使用匹配的父类参数名称。

详情

#

应该 使用与其对应的父类构造函数的参数名称相匹配的父类参数名称。

错误示例

dart
class Rectangle {
  final int width;
  final int height;

  Rectangle(this.width, this.height);
}

class ColoredRectangle extends Rectangle {
  final Color color;

  ColoredRectangle(
    this.color,
    super.height, // Bad, actually corresponds to the `width` parameter.
    super.width, // Bad, actually corresponds to the `height` parameter.
  );
}

正确示例

dart
class Rectangle {
  final int width;
  final int height;

  Rectangle(this.width, this.height);
}

class ColoredRectangle extends Rectangle {
  final Color color;

  ColoredRectangle(
    this.color,
    super.width,
    super.height,
  );
}

启用

#

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

analysis_options.yaml
yaml
linter:
  rules:
    - matching_super_parameters

如果你改为使用 YAML 映射语法来配置代码检查规则,请在 linter > rules 下添加 matching_super_parameters: true

analysis_options.yaml
yaml
linter:
  rules:
    matching_super_parameters: true