Dart Analyze logoDart Analyze/
DRT-W1378

Incompatible types for getter and setterDRT-W1378

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic when the return type of a getter isn't a subtype of the type of the parameter of a setter with the same name.

The subtype relationship is a requirement whether the getter and setter are in the same class or whether one of them is in a superclass of the other.

Example

The following code produces this diagnostic because the return type of the getter x is num, the parameter type of the setter x is int, and num isn't a subtype of int:

class C {
  num get x => 0;

  set x(int y) {}
}

Common fixes

If the type of the getter is correct, then change the type of the setter:

class C {
  num get x => 0;

  set x(num y) {}
}

If the type of the setter is correct, then change the type of the getter:

class C {
  int get x => 0;

  set x(int y) {}
}