Dart Tutorials

Dart List.from()
Syntax & Examples

Syntax of List.from

The syntax of List.List.from constructor is:

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

This List.from constructor of List creates a list containing all elements.

Parameters

ParameterOptional/RequiredDescription
elementsrequiredthe elements used to create the list
growableoptional [default value is true]whether the list is growable


✐ Examples

1 Create a list from numbers

In this example,

  1. We create a list named numbers by calling List.from() with elements [1, 2, 3].
  2. Since no growable parameter is provided, the list is growable by default.
  3. We print the list to standard output.

Dart Program

void main() {
  List<int> numbers = List.from([1, 2, 3]);
  print(numbers); // Output: [1, 2, 3]
}

Output

[1, 2, 3]

2 Create a list from characters with non-growable list

In this example,

  1. We create a list named characters by calling List.from() with elements ['a', 'b', 'c'] and setting growable parameter to false.
  2. As a result, the list is created with fixed size and cannot be modified after creation.
  3. We print the list to standard output.

Dart Program

void main() {
  List<String> characters = List.from(['a', 'b', 'c'], growable: false);
  print(characters); // Output: [a, b, c]
}

Output

[a, b, c]

3 Create a list from strings

In this example,

  1. We create a list named strings by calling List.from() with elements ['apple', 'banana', 'cherry'].
  2. Since no growable parameter is provided, the list is growable by default.
  3. We print the list to standard output.

Dart Program

void main() {
  List<String> strings = List.from(['apple', 'banana', 'cherry']);
  print(strings); // Output: [apple, banana, cherry]
}

Output

[apple, banana, cherry]

Summary

In this Dart tutorial, we learned about List.from constructor of List: the syntax and few working examples with output and detailed explanation for each example.