Skip to main content

prefer-returning-conditional-expressions

added in: 1.5.0
🛠
Pro+

Warns when several returns inside a function body can be replace with a single conditional expression.

Example

❌ Bad:

const _value = 1;

int? anotherOne() {
final value = 1;
if (value == 2) {
return value;
}

return null; // LINT: Prefer returning conditional expressions.
}

✅ Good:

int? anotherOne() {
final value = 1;

return value == 2 ? value : null;
}

void correct() {
if (_value != 2) {
return _value;
}
final value = 1;

return value;
}

void correct() {
final value = 1;
if (_value != 2) {
print(_value);
}

return;
}