prefer_for_elements_to_map_fromIterable

Group: style

Maturity: stable

Dart SDK: >= 2.3.0 • (Linter v0.1.85)

Since info is static, may be stale
recommendedflutterhas-fix

View all Lint Rules

Using the Linter

When building maps from iterables, it is preferable to use 'for' elements.

Using 'for' elements brings several benefits including:

BAD:

Map<String, WidgetBuilder>.fromIterable(
  kAllGalleryDemos,
  key: (demo) => '${demo.routeName}',
  value: (demo) => demo.buildRoute,
);

GOOD:

return {
  for (var demo in kAllGalleryDemos)
    '${demo.routeName}': demo.buildRoute,
};

GOOD:

// Map<int, Student> is not required, type is inferred automatically.
final pizzaRecipients = {
  ...studentLeaders,
  for (var student in classG)
    if (student.isPassing) student.id: student,
};