type_init_formals
Don't needlessly type annotate initializing formals.
Description
#The analyzer produces this diagnostic when an initializing formal
parameter (this.x
) or a super parameter (super.x
) has an explicit type
annotation that is the same as the field or overridden parameter.
If a constructor parameter is using this.x
to initialize a field, then
the type of the parameter is implicitly the same type as the field. If a
constructor parameter is using super.x
to forward to a super
constructor, then the type of the parameter is implicitly the same as the
super constructor parameter.
Example
#The following code produces this diagnostic because the parameter this.c
has an explicit type that is the same as the field c
:
class C {
int c;
C(int this.c);
}
The following code produces this diagnostic because the parameter
super.a
has an explicit type that is the same as the parameter a
from
the superclass:
class A {
A(int a);
}
class B extends A {
B(int super.a);
}
Common fixes
#Remove the type annotation from the parameter:
class C {
int c;
C(this.c);
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。