The syntax of List.add() method is:
void add(E value)
This add() method of List adds value
to the end of this list, extending the length by one.
Parameter | Optional/Required | Description |
---|---|---|
value | required | the value to add to the list |
List.add() returns value of type void
.
In this example,
numbers
containing integers [1, 2, 3].add()
method to add the number 4 to the list.void main() {
List<int> numbers = [1, 2, 3];
numbers.add(4);
print(numbers); // Output: [1, 2, 3, 4]
}
[1, 2, 3, 4]
In this example,
characters
containing characters ['a', 'b', 'c'].add()
method to add the character 'd' to the list.void main() {
List<String> characters = ['a', 'b', 'c'];
characters.add('d');
print(characters); // Output: [a, b, c, d]
}
[a, b, c, d]
In this example,
strings
containing strings ['apple', 'banana'].add()
method to add the string 'cherry' to the list.void main() {
List<String> strings = ['apple', 'banana'];
strings.add('cherry');
print(strings); // Output: [apple, banana, cherry]
}
[apple, banana, cherry]
In this Dart tutorial, we learned about add() method of List: the syntax and few working examples with output and detailed explanation for each example.