List<E>.from constructor Null safety

  1. Iterable elements,
  2. {bool growable = true}

Creates a list containing all elements.

The Iterator of elements provides the order of the elements.

All the elements should be instances of E.


final numbers = <num>[1, 2, 3];
final listFrom = List<int>.from(numbers);
print(listFrom); // [1, 2, 3]

The elements iterable itself may have any element type, so this constructor can be used to down-cast a List, for example as:

const jsonArray = '''
  [{"text": "foo", "value": 1, "status": true},
   {"text": "bar", "value": 2, "status": false}]
final List<dynamic> dynamicList = jsonDecode(jsonArray);
final List<Map<String, dynamic>> fooData =
    List.from(dynamicList.where((x) => x is Map && x['text'] == 'foo'));
print(fooData); // [{text: foo, value: 1, status: true}]

This constructor creates a growable list when growable is true; otherwise, it returns a fixed-length list.


external factory List.from(Iterable elements, {bool growable = true});