Skip to main content

avoid-unconditional-break

Warns when a break, continue, return or throw are used unconditionally in a for loop.

Example

❌ Bad:

void main() {
final list = <int>[];

for (final item in list) {
// LINT: Avoid unconditional break statements. This loop is always interrupted after one iteration.
// Try removing this statement or call it conditionally.
continue;
}

for (final item in list) {
// LINT: Avoid unconditional break statements. This loop is always interrupted after one iteration.
// Try removing this statement or call it conditionally.
break;
}

for (final item in list) {
// LINT: Avoid unconditional break statements. This loop is always interrupted after one iteration.
// Try removing this statement or call it conditionally.
return;
}

for (final item in list) {
// LINT: Avoid unconditional break statements. This loop is always interrupted after one iteration.
// Try removing this statement or call it conditionally.
throw '';
}
}

✅ Good:

void main() {
final list = <int>[];

for (final item in list) {
if (someCondition) { // Correct, only in some cases
continue;
}
}

for (final item in list) {
if (someCondition) {
break;
}
}

for (final item in list) {
if (someCondition) {
return;
}
}

for (final item in list) {
if (someCondition) {
throw '';
}
}
}

Additional Resources