type_init_formals
Don't type annotate initializing formals.
This rule is available as of Dart 2.0.
Rule sets: recommended, flutter
This rule has a quick fix available.
Details
#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:
class Point {
int x, y;
Point(int this.x, int this.y);
}
GOOD:
class Point {
int x, y;
Point(this.x, this.y);
}
BAD:
class A {
int a;
A(this.a);
}
class B extends A {
B(int super.a);
}
GOOD:
class A {
int a;
A(this.a);
}
class B extends A {
B(super.a);
}
Usage
#To enable the type_init_formals
rule,
add type_init_formals
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- type_init_formals
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。