Create a list from an iterable with a known element type.
The List.of constructor creates a new list from the given iterable, preserving the element type without implicit downcasting. It is the preferred alternative to List.from when the element type matches the target list type.
Unlike List.from, List.of does not perform any type coercion. If the source iterable's type does not match, the error occurs at compile time rather than at runtime, making code safer and easier to debug.
List.of is typically used when you need a copy of an existing list or want to convert a typed iterable to a list while maintaining type safety throughout.
| Name | Description | Optional |
|---|---|---|
elements |
The iterable whose elements are copied into the new list. | No |
growable |
Whether the list is allowed to grow. Defaults to true. | Yes |
var original = <int>[3, 1, 2];
var copy = List.of(original);
print(copy); // [3, 1, 2]
var set = <String>{'a', 'b', 'c'};
var list = List.of(set);
print(list); // [a, b, c]