Skip to main content

prefer-iterable-of

added in: 1.6.0
🛠
Free+
preset: recommended

Warns when the List.from() factory is used instead of List.of().

The difference between List.of() and List.from() is that .of() takes an argument of the same type as what it returns and enforces it at compilation time, and that .from() allows potentially unsafe downcasting and enforces convertibility at runtime.

Example

❌ Bad:

...
var intList = [1, 2, 3];

var copy = List<int>.from(intList); // LINT: Prefer '.of' instead of 'from'.
var numList = List<num>.from(intList); // LINT: Prefer '.of' instead of 'from'.

var unspecifiedList = List.from(intList); // LINT: Prefer '.of' instead of 'from'.

final recordsSet = <(num,)>{(1,), (2,), (3,)};
final recordSet = Set<(num,)>.from(recordsSet); // LINT: Prefer '.of' instead of 'from'.
final extra = Set<(int, String)>.from(recordsSet); // LINT: Prefer '.of' instead of 'from'.

✅ Good:

var intList = [1, 2, 3];

var copy = List<int>.of(intList);
var numList = List<num>.of(intList);
...

var numList = <num>[1, 2, 3];

var intList = List<int>.from(numList);

final recordsSet = <(num,)>{(1,), (2,), (3,)};
final record = Set<(int,)>.from(recordsSet);

Additional Resources