跳到主要內容

matching_super_parameters

穩定

使用相符的 super 參數名稱。

詳細資訊

#

應該 使用與其對應的 super 建構子的參數名稱相符的 super 參數名稱。

不良範例

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 map 語法來設定 linter 規則,請在 linter > rules 下新增 matching_super_parameters: true

analysis_options.yaml
yaml
linter:
  rules:
    matching_super_parameters: true