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);
}
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.