DeepSource has joined Harness. Learn more
Dart Analyze logoDart Analyze/
DRT-W1369

Duplicate initialization of final fieldDRT-W1369

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic when a final field is initialized twice: once where it's declared and once by a constructor's parameter.

Example

The following code produces this diagnostic because the field f is initialized twice:

class C {
  final int f = 0;

  C(this.f);
}

Common fixes

If the field should have the same value for all instances, then remove the initialization in the parameter list:

class C {
  final int f = 0;

  C();
}

If the field can have different values in different instances, then remove the initialization in the declaration:

class C {
  final int f;

  C(this.f);
}