avoid-function-literals-in-foreach-calls
Same as avoid_function_literals_in_foreach_calls in Dart lints
- Group
- Complexity
- Path
lint/complexity/avoid-function-literals-in-foreach-calls- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 66 of 512
Flags a function literal passed to an Iterable's forEach.
A for-in loop reads more clearly than forEach with a closure, and unlike
the closure it can await, break, and continue. Stream and Map receivers,
tear-offs, null-aware chains, and receivers reached through method calls are
left alone, matching the SDK lint.
Invalid
void bad(List<int> items, List<int> list, List<int> names, List<int> values,
List<int> data, void Function(int) save, void Function(int) sink,
void Function(int) process, void Function(int) log) {
items.forEach((e) { print(e); });
list.forEach((x) => print(x));
names.forEach((name) { save(name); });
values.forEach((v) => sink(v));
data.forEach((item) { process(item); log(item); });
} void Function(int) process, void Function(int) log) { items.forEach((e) { print(e); }); items.forEach((e) { print(e); }); list.forEach((x) => print(x)); list.forEach((x) => print(x)); names.forEach((name) { save(name); }); names.forEach((name) { save(name); }); values.forEach((v) => sink(v)); values.forEach((v) => sink(v)); data.forEach((item) { process(item); log(item); });Valid
void good(List<int> items, List<int>? list, Map<int, int> map, List<int> numbers) {
items.forEach(print);
list?.forEach((e) => print(e));
map.forEach((k, v) => print('$k$v'));
for (final e in items) {
print(e);
}
numbers.map((e) => e * 2);
}Configure avoid-function-literals-in-foreach-calls
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"avoid-function-literals-in-foreach-calls": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/avoid-function-literals-in-foreach-calls: <reason>More complexity rules
- avoid-if-with-many-branches
Reports long if/else-if chains; replace them with a switch or extracted helpers.
- avoid-inverted-boolean-expressions
avoid_inverted_boolean_expressionsrecommendedFlags a doubly-negated boolean expression such as
!!x. - avoid-long-records
Reports record values and record types with too many fields.
- avoid-nested-conditional-expressions
avoid-nested-conditional-expressionsFlags a ternary conditional expression nested inside another ternary.
- avoid-nested-if
avoid_nested_ifFlags an
ifstatement whose then-branch contains nestedifstatements. - avoid-nested-switch-expressions
Reports deeply nested switch expressions.
- avoid-nested-switches
Reports deeply nested switch statements.
- avoid-nested-try-statements
Reports deeply nested try statements.