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
Parameter | Optional/Required | Description |
---|---|---|
elements | required | the elements to be included in the unmodifiable list |
✐ Examples
1 Create an unmodifiable list of numbers
In this example,
- We create an unmodifiable list named
numbers
containing the integers1, 2, 3
usingList.unmodifiable
. - 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,
- We create an unmodifiable list named
characters
containing the characters'a', 'b', 'c'
usingList.unmodifiable
. - 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,
- We create an unmodifiable list named
words
containing the strings'apple', 'banana', 'cherry'
usingList.unmodifiable
. - 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.