跳到主要內容

匹配超類引數

穩定

使用匹配的超類引數名稱。

詳細資訊

#

請務必使用與對應超類建構函式引數名稱匹配的超類引數名稱。

錯誤示例

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 規則,請在 linter > rules 下新增 matching_super_parameters: true

analysis_options.yaml
yaml
linter:
  rules:
    matching_super_parameters: true