Skip to main content

prefer-for-loop-in-children

added in: 1.16.0
Pro+

Suggests using for-loop in arguments that accept a list of widgets.

Example

❌ Bad:

Column(
// LINT: Prefer for-loops in arguments that accept a list of widgets. Try rewriting this expression to be a for-loop.
children: exampleList.map((example) => const Text(example)).toList(),
);

Column(
// LINT: Prefer for-loops in arguments that accept a list of widgets. Try rewriting this expression to be a for-loop.
children: [
...exampleList.map((example) => const Text(example)).toList(),
],
);

Column(
// LINT: Prefer for-loops in arguments that accept a list of widgets. Try rewriting this expression to be a for-loop.
children: List.generate(
exampleList.length,
(index) => Text(exampleList[index]),
),
);

Column(
// LINT: Prefer for-loops in arguments that accept a list of widgets. Try rewriting this expression to be a for-loop.
children: exampleList.fold([], (list, example) {
list.add(Text(example));
return list;
}),
);

✅ Good:

Column(
children: [
for (final example in exampleList) Text(example),
],
);