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
flutter

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

example.dartdart
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());
Use a collection-for element instead of `map(...).toList()` in children.
3
4Widget direct(List<String> labels) => Column(children: labels.map((label) => Text(label)).toList());
Use a collection-for element instead of `map(...).toList()` in children.
4Widget direct(List<String> labels) => Column(children: labels.map((label) => Text(label)).toList());
5Widget row(Set<String> labels) => new Row(children: labels.map((label) => Text(label)).toList());
Use a collection-for element instead of `map(...).toList()` in children.
5Widget row(Set<String> labels) => new Row(children: labels.map((label) => Text(label)).toList());
6Widget prefixed(Iterable<String> labels) => m.Column(children: labels.map((label) => m.Text(label)).toList());
Use a collection-for element instead of `map(...).toList()` in children.
6Widget prefixed(Iterable<String> labels) => m.Column(children: labels.map((label) => m.Text(label)).toList());
7Widget listView(List<String> labels) => ListView(children: labels.map<Widget>((label) => Text(label)).toList());
Use a collection-for element instead of `map(...).toList()` in children.
7Widget listView(List<String> labels) => ListView(children: labels.map<Widget>((label) => Text(label)).toList());
8Widget literal() => Column(children: ['one', 'two'].map((label) => Text(label)).toList());

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-for-loop-in-children": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/prefer-for-loop-in-children: <reason>
  • prefer-function-declarations-over-variablesprefer_function_declarations_over_variablesrecommended

    Flags a local variable bound to a function literal that should be a local function declaration.

  • prefer-generic-function-type-aliasesprefer_generic_function_type_aliasesrecommended

    Flags 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-formalsprefer_initializing_formalsrecommended

    Prefer initializing formals when a parameter is copied directly into its field.

  • prefer-inlined-addsprefer_inlined_addsrecommended

    Flags an add cascade on a list literal whose element could be written inline.

  • prefer-int-literalsprefer_int_literals

    Flags a whole-number double literal, such as 1.0, where the context type is double so the int literal 1 means the same value.

  • prefer-interpolation-to-compose-stringsprefer_interpolation_to_compose_stringsrecommended

    Prefers string interpolation to composing strings with +.

  • prefer-is-emptyprefer_is_emptyrecommended

    Flags .length comparisons equivalent to a .isEmpty check.