Dart Analyze logoDart Analyze/
DRT-W1411

Incompatible types for variable assignmentDRT-W1411

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic when the static type of an expression that is assigned to a variable isn't assignable to the type of the variable.

Example

The following code produces this diagnostic because the type of the initializer (int) isn't assignable to the type of the variable (String):

int i = 0;
String s = i;

Common fixes

If the value being assigned is always assignable at runtime, even though the static types don't reflect that, then add an explicit cast.

Otherwise, change the value being assigned so that it has the expected type. In the previous example, this might look like:

int i = 0;
String s = i.toString();

If you can't change the value, then change the type of the variable to be compatible with the type of the value being assigned:

int i = 0;
int s = i;