Dart Analyze logoDart Analyze/
DRT-W1366

Incompatible types for field and initializing formal parameterDRT-W1366

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic when the type of an initializing formal parameter isn't assignable to the type of the field being initialized.

Example

The following code produces this diagnostic because the initializing formal parameter has the type String, but the type of the field is int. The parameter must have a type that is a subtype of the field's type.

class C {
  int f;

  C(String this.f);
}

Common fixes

If the type of the field is incorrect, then change the type of the field to match the type of the parameter, and consider removing the type from the parameter:

class C {
  String f;

  C(this.f);
}

If the type of the parameter is incorrect, then remove the type of the parameter:

class C {
  int f;

  C(this.f);
}

If the types of both the field and the parameter are correct, then use an initializer rather than an initializing formal parameter to convert the parameter value into a value of the correct type:

class C {
  int f;

  C(String s) : f = int.parse(s);
}