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);
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.