field_initializer_redirecting_constructor
The redirecting constructor can't have a field initializer.
Description
#The analyzer produces this diagnostic when a redirecting constructor initializes a field in the object. This isn't allowed because the instance that has the field hasn't been created at the point at which it should be initialized.
Examples
#The following code produces this diagnostic because the constructor
C.zero
, which redirects to the constructor C
, has an initializing
formal parameter that initializes the field f
:
class C {
int f;
C(this.f);
C.zero(this.f) : this(f);
}
The following code produces this diagnostic because the constructor
C.zero
, which redirects to the constructor C
, has an initializer that
initializes the field f
:
class C {
int f;
C(this.f);
C.zero() : f = 0, this(1);
}
Common fixes
#If the initialization is done by an initializing formal parameter, then use a normal parameter:
class C {
int f;
C(this.f);
C.zero(int f) : this(f);
}
If the initialization is done in an initializer, then remove the initializer:
class C {
int f;
C(this.f);
C.zero() : this(0);
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。