Rules / Style / prefer-iterable-of

prefer-iterable-of

lint/style/prefer-iterable-of
recommended
Same as prefer-iterable-of · Dart Code Metrics

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);
}
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);

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);
}

How to configure

Set the severity of prefer-iterable-of in your falcon.json:

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