Dart Analyze logoDart Analyze/
DRT-W1268

Can't define a const constructor for a class with non-final fieldsDRT-W1268

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic when a constructor is marked as a const constructor, but the constructor is defined in a class that has at least one non-final instance field (either directly or by inheritance).

Example

The following code produces this diagnostic because the field x isn't final:

class C {
  int x;

  const C(this.x);
}

Common fixes

If it's possible to mark all of the fields as final, then do so:

class C {
  final int x;

  const C(this.x);
}

If it isn't possible to mark all of the fields as final, then remove the keyword const from the constructor:

class C {
  int x;

  C(this.x);
}