initializer_for_static_field
'{0}' is a static field in the enclosing class. Fields initialized in a constructor can't be static.
Description
#The analyzer produces this diagnostic when a static field is initialized in a constructor using either an initializing formal parameter or an assignment in the initializer list.
Example
#The following code produces this diagnostic because the static field a
is being initialized by the initializing formal parameter this.a
:
class C {
static int? a;
C(this.a);
}
Common fixes
#If the field should be an instance field, then remove the keyword static
:
class C {
int? a;
C(this.a);
}
If you intended to initialize an instance field and typed the wrong name, then correct the name of the field being initialized:
class C {
static int? a;
int? b;
C(this.b);
}
If you really want to initialize the static field, then move the initialization into the constructor body:
class C {
static int? a;
C(int? c) {
a = 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.