field_initialized_in_parameter_and_initializer
Fields can't be initialized in both the parameter list and the initializers.
Description
#The analyzer produces this diagnostic when a field is initialized in both the parameter list and in the initializer list of a constructor.
Example
#The following code produces this diagnostic because the field f
is
initialized both by an initializing formal parameter and in the
initializer list:
class C {
int f;
C(this.f) : f = 0;
}
Common fixes
#If the field should be initialized by the parameter, then remove the initialization in the initializer list:
class C {
int f;
C(this.f);
}
If the field should be initialized in the initializer list and the parameter isn't needed, then remove the parameter:
class C {
int f;
C() : f = 0;
}
If the field should be initialized in the initializer list and the parameter is needed, then make it a normal parameter:
class C {
int f;
C(int g) : f = g * 2;
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。