prefer-for-loop-in-children
- Group
- Style
- Path
lint/style/prefer-for-loop-in-children- Default
- Off, opt in
- Origin
- Falcon
- Domain
- flutter
- Rule
- 361 of 512
Prefers collection-for elements over eager map/toList chains in Flutter children.
Direct children arguments are checked on resolved Flutter constructors.
Spreads are left to unnecessary-to-list-in-spreads to avoid duplicate reports.
The map receiver must be a proven
non-null SDK List, Set, or Iterable, with a single untyped positional parameter
and a synchronous arrow body. Named arguments, typed callbacks, async or block
bodies, null-aware calls, and custom collection APIs are left unchanged. The
suggestion retains eager evaluation and element order; no automatic fix is made.
Invalid
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as m;
Widget direct(List<String> labels) => Column(children: labels.map((label) => Text(label)).toList());
Widget row(Set<String> labels) => new Row(children: labels.map((label) => Text(label)).toList());
Widget prefixed(Iterable<String> labels) => m.Column(children: labels.map((label) => m.Text(label)).toList());
Widget listView(List<String> labels) => ListView(children: labels.map<Widget>((label) => Text(label)).toList());
Widget literal() => Column(children: ['one', 'two'].map((label) => Text(label)).toList()); Widget direct(List<String> labels) => Column(children: labels.map((label) => Text(label)).toList());Widget direct(List<String> labels) => Column(children: labels.map((label) => Text(label)).toList());Widget row(Set<String> labels) => new Row(children: labels.map((label) => Text(label)).toList());Widget row(Set<String> labels) => new Row(children: labels.map((label) => Text(label)).toList());Widget prefixed(Iterable<String> labels) => m.Column(children: labels.map((label) => m.Text(label)).toList());Widget prefixed(Iterable<String> labels) => m.Column(children: labels.map((label) => m.Text(label)).toList());Widget listView(List<String> labels) => ListView(children: labels.map<Widget>((label) => Text(label)).toList());Widget listView(List<String> labels) => ListView(children: labels.map<Widget>((label) => Text(label)).toList());Widget literal() => Column(children: ['one', 'two'].map((label) => Text(label)).toList());Valid
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as m;
Widget loop(List<String> labels) => Column(children: [for (final label in labels) Text(label)]);
Widget block(List<String> labels) => Column(children: labels.map((label) { return Text(label); }).toList());
Widget typed(List<String> labels) => Column(children: labels.map((String label) => Text(label)).toList());
Widget tearOff(List<String> labels) => Column(children: labels.map(Text.new).toList());
Widget growable(List<String> labels) => Column(children: labels.map((label) => Text(label)).toList(growable: false));
Widget unknown(dynamic labels) => Column(children: labels.map((label) => Text(label)).toList());
Widget shadow(dynamic Column, List<String> labels) => Column(children: labels.map((label) => Text(label)).toList());
Widget prefixShadow(dynamic m, List<String> labels) => m.Column(children: labels.map((label) => Text(label)).toList());
Widget nullable(List<String>? labels) => Column(children: labels?.map((label) => Text(label)).toList() ?? []);
Widget custom(Custom labels) => Column(children: labels.map((label) => Text(label)).toList());
class Custom {
Custom map(Widget Function(String) callback) => this;
List<Widget> toList() => [];
}
Widget spread(List<String> labels) => Column(children: [
...labels.map((label) => Text(label)).toList(),
]);Configure prefer-for-loop-in-children
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-for-loop-in-children": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-for-loop-in-children: <reason>More style rules
- prefer-function-declarations-over-variables
prefer_function_declarations_over_variablesrecommendedFlags a local variable bound to a function literal that should be a local function declaration.
- prefer-generic-function-type-aliases
prefer_generic_function_type_aliasesrecommendedFlags old-style function typedefs written with inline parameter syntax.
- prefer-getter-over-method
Prefer getters for synchronous, zero-argument methods that only read a field.
- prefer-initializing-formals
prefer_initializing_formalsrecommendedPrefer initializing formals when a parameter is copied directly into its field.
- prefer-inlined-adds
prefer_inlined_addsrecommendedFlags an
addcascade on a list literal whose element could be written inline. - prefer-int-literals
prefer_int_literalsFlags a whole-number double literal, such as
1.0, where the context type isdoubleso the int literal1means the same value. - prefer-interpolation-to-compose-strings
prefer_interpolation_to_compose_stringsrecommendedPrefers string interpolation to composing strings with
+. - prefer-is-empty
prefer_is_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isEmptycheck.