Skip to main content

avoid-assignments-as-conditions

added in: 1.22.0
Pro+
preset: recommended

Warns when an assignment is used inside a condition.

Assigning to a variable inside a condition can be confusing or indicate an incorrect operator (= instead of ==). Consider replacing it with == or moving the assignment out of the condition to clearly indicate that this is intentional.

Example

❌ Bad:

void fn(List<String> values) {
bool? flag;

if (flag ??= values.isEmpty) {} // LINT: Avoid assignments as conditions. Try moving this assignment out.
if (flag = values.isEmpty) {} // LINT: Avoid assignments as conditions. Try moving this assignment out.
}

✅ Good:

void fn(List<String> values) {
bool? flag;

flag ??= values.isEmpty; // Correct, assigning outside of the condition
if (flag) {
...
}
}

Additional Resources