Skip to main content

initialize_in_field_declaration

Details about the 'initialize_in_field_declaration' diagnostic produced by the Dart analyzer.

Field should be initialized in the field declaration.

Description

#

The analyzer produces this diagnostic when a field initialized in either the initializer list or body of a primary constructor can be initialized in the field declaration.

Example

#

The following code produces this diagnostic because the field y is initialized in the primary constructor's initializer list, when it can be initialized in the field declaration:

dart
class C(int x) {
  this : y = x;

  int y;
}

Common fixes

#

Initialize the field in its declaration:

dart
class C(int x) {
  int y = x;
}