prefer-iterable-of

Same as prefer-iterable-of in Dart Code Metrics

Group
Style
Path
lint/style/prefer-iterable-of
Default
On (recommended)
Origin
Dart Code Metrics
Rule
381 of 512
recommended

Flags .from collection constructors in favor of the .of constructor.

Catches List.from, Set.from, and Iterable.from — whether written as a static call (List.from(xs), List<int>.from(xs)) or with new. The .from constructor takes an Iterable<dynamic> and re-types its elements, so a type mismatch surfaces only as a runtime cast failure; the .of constructor takes a statically typed Iterable<E>, letting the compiler reject bad element types up front. Prefer .of unless you specifically need .from's dynamic widening.

Invalid

example.dartdart
void example(List<int> other, Set<String> names) {
  final a = List<int>.from(other);
  final b = Set<String>.from(names);
  final c = List.from(other);
}

class Repo {
  List<int> copy(Iterable<int> src) {
    return List<int>.from(src);
  }

  void copySet(Iterable<String> items) {
    final s = Set<String>.from(items);
  }

  Iterable<dynamic> copyIterable(Iterable<dynamic> data) {
    return Iterable.from(data);
  }
}

void nestedConstructors(List<int> values) {
  final outer = List<int>.from(List.from(values));
  final inner = List<int>.from(List.from(values));
}

// Legacy explicit `new` constructor form is still flagged.
void legacyNew(List<int> other, Set<String> names) {
  final a = new List<int>.from(other);
  final b = new Set.from(names);
  final c = new Iterable.from(other);
}

// Regression: the violation must still be found inside Dart 3 containers
// (pattern declaration, labeled statement, switch expression and subject,
// collection if/spread, record field).
void containersRegression(int rcount, List<int> other) {
  final (ra, _) = (List<int>.from(other), 0);
  lbl: {
    final rb = List<int>.from(other);
    print(rb);
  }
  final rc = switch (rcount) {
    0 => List<int>.from(other),
    _ => null,
  };
  final rd = switch (List<int>.from(other)) {
    _ => 0,
  };
  final re = [if (rcount > 0) List<int>.from(other)];
  final rf = [...[List<int>.from(other)]];
  final rg = (p: List<int>.from(other), q: 0);
  print([ra, rc, rd, re, rf, rg]);
}
Prefer using the `of` constructor instead of `from`.
1void example(List<int> other, Set<String> names) {
2 final a = List<int>.from(other);
Prefer using the `of` constructor instead of `from`.
2 final a = List<int>.from(other);
3 final b = Set<String>.from(names);
Prefer using the `of` constructor instead of `from`.
3 final b = Set<String>.from(names);
4 final c = List.from(other);
Prefer using the `of` constructor instead of `from`.
8 List<int> copy(Iterable<int> src) {
9 return List<int>.from(src);
Prefer using the `of` constructor instead of `from`.
12 void copySet(Iterable<String> items) {
13 final s = Set<String>.from(items);
Prefer using the `of` constructor instead of `from`.
16 Iterable<dynamic> copyIterable(Iterable<dynamic> data) {
17 return Iterable.from(data);
Prefer using the `of` constructor instead of `from`.
21void nestedConstructors(List<int> values) {
22 final outer = List<int>.from(List.from(values));
Prefer using the `of` constructor instead of `from`.
21void nestedConstructors(List<int> values) {
22 final outer = List<int>.from(List.from(values));
Prefer using the `of` constructor instead of `from`.
22 final outer = List<int>.from(List.from(values));
23 final inner = List<int>.from(List.from(values));
Prefer using the `of` constructor instead of `from`.
22 final outer = List<int>.from(List.from(values));
23 final inner = List<int>.from(List.from(values));
Prefer using the `of` constructor instead of `from`.
27void legacyNew(List<int> other, Set<String> names) {
28 final a = new List<int>.from(other);
Prefer using the `of` constructor instead of `from`.
28 final a = new List<int>.from(other);
29 final b = new Set.from(names);
Prefer using the `of` constructor instead of `from`.
29 final b = new Set.from(names);
30 final c = new Iterable.from(other);
Prefer using the `of` constructor instead of `from`.
36void containersRegression(int rcount, List<int> other) {
37 final (ra, _) = (List<int>.from(other), 0);
Prefer using the `of` constructor instead of `from`.
38 lbl: {
39 final rb = List<int>.from(other);
Prefer using the `of` constructor instead of `from`.
42 final rc = switch (rcount) {
43 0 => List<int>.from(other),
Prefer using the `of` constructor instead of `from`.
45 };
46 final rd = switch (List<int>.from(other)) {
Prefer using the `of` constructor instead of `from`.
48 };
49 final re = [if (rcount > 0) List<int>.from(other)];
Prefer using the `of` constructor instead of `from`.
49 final re = [if (rcount > 0) List<int>.from(other)];
50 final rf = [...[List<int>.from(other)]];
Prefer using the `of` constructor instead of `from`.
50 final rf = [...[List<int>.from(other)]];
51 final rg = (p: List<int>.from(other), q: 0);

Valid

example.dartdart
void example(List<int> other, Set<String> names) {
  final a = List<int>.of(other);
  final b = Set<String>.of(names);
  final c = List.of(other);
}

class Repo {
  List<int> copy(Iterable<int> src) {
    return List<int>.of(src);
  }

  void copySet(Iterable<String> items) {
    final s = Set<String>.of(items);
  }

  Iterable<dynamic> copyIterable(Iterable<dynamic> data) {
    return Iterable.of(data);
  }
}

void spreadOperator(List<int> values) {
  final list = [...values];
}

void mapFromIsOk() {
  final map = Map.from({1: 'a', 2: 'b'});
}

void plainConstructors() {
  final a = List<int>();
  final b = Set<String>();
  final c = Iterable<int>();
}

void newOfIsOk(List<int> other) {
  final a = new List<int>.of(other);
  final b = new Set.of(other);
}

void newNonIterableFromIsOk(Map<int, String> m) {
  final a = new Map.from(m);
  final b = new MyThing.from(m);
}

Configure prefer-iterable-of

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-iterable-of": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-iterable-of: <reason>