Dart List.of()
Syntax & Examples


Syntax of List.of

The syntax of List.List.of constructor is:

List.of(Iterable<E> elements, {bool growable = true})

This List.of constructor of List creates a list from 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.of() 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&lt;int&gt; numbers = List.of([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.of() 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&lt;String&gt; characters = List.of(['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.of() 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&lt;String&gt; strings = List.of(['apple', 'banana', 'cherry']);
  print(strings); // Output: [apple, banana, cherry]
}

Output

[apple, banana, cherry]

Summary

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