Skip to main content

prefer-iterable-of

added in: 4.19.0
warning
🛠

Warns when 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.

Additional resources:

Example

❌ Bad:

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

var copy = List<int>.from(intList); // LINT
var numList = List<num>.from(intList); // LINT

var unspecifedList = List.from(intList); // LINT

✅ Good:

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

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