目录

type_init_formals

Don't type annotate initializing formals.

此规则自 Dart 2.0 版本起可用。

_规则集:recommended , flutter _

此规则提供 快速修复

详情

#

From Effective Dart:

DON'T type annotate initializing formals.

If a constructor parameter is using this.x to initialize a field, then the type of the parameter is understood to be the same type as the field. If a a constructor parameter is using super.x to forward to a super constructor, then the type of the parameter is understood to be the same as the super constructor parameter.

Type annotating an initializing formal with a different type than that of the field is OK.

BAD:

dart
class Point {
  int x, y;
  Point(int this.x, int this.y);
}

GOOD:

dart
class Point {
  int x, y;
  Point(this.x, this.y);
}

BAD:

dart
class A {
  int a;
  A(this.a);
}

class B extends A {
  B(int super.a);
}

GOOD:

dart
class A {
  int a;
  A(this.a);
}

class B extends A {
  B(super.a);
}

使用方法

#

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

analysis_options.yaml
yaml
linter:
  rules:
    - type_init_formals