Dart List.unmodifiable()
Syntax & Examples


Syntax of List.unmodifiable

The syntax of List.List.unmodifiable constructor is:

List.unmodifiable(Iterable elements)

This List.unmodifiable constructor of List creates an unmodifiable list containing all elements.

Parameters

ParameterOptional/RequiredDescription
elementsrequiredthe elements to be included in the unmodifiable list


✐ Examples

1 Create an unmodifiable list of numbers

In this example,

  1. We create an unmodifiable list named numbers containing the integers 1, 2, 3 using List.unmodifiable.
  2. We print the numbers list to standard output.

Dart Program

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

Output

[1, 2, 3]

2 Create an unmodifiable list of characters

In this example,

  1. We create an unmodifiable list named characters containing the characters 'a', 'b', 'c' using List.unmodifiable.
  2. We print the characters list to standard output.

Dart Program

void main() {
  List<String> characters = List.unmodifiable(['a', 'b', 'c']);
  print(characters);
}

Output

[a, b, c]

3 Create an unmodifiable list of strings

In this example,

  1. We create an unmodifiable list named words containing the strings 'apple', 'banana', 'cherry' using List.unmodifiable.
  2. We print the words list to standard output.

Dart Program

void main() {
  List<String> words = List.unmodifiable(['apple', 'banana', 'cherry']);
  print(words);
}

Output

[apple, banana, cherry]

Summary

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