The syntax of List.addAll() method is:
void addAll(Iterable<E> iterable)
This addAll() method of List appends all objects of iterable
to the end of this list.
Parameter | Optional/Required | Description |
---|---|---|
iterable | required | the iterable whose elements will be added to the list |
List.addAll() returns value of type void
.
In this example,
numbers
containing the integers 1, 2, 3
.additionalNumbers
containing the integers 4, 5, 6
.addAll()
method to append all elements from additionalNumbers
to the end of numbers
.void main() {
List<int> numbers = [1, 2, 3];
List<int> additionalNumbers = [4, 5, 6];
numbers.addAll(additionalNumbers);
print('Combined list: $numbers'); // Output: Combined list: [1, 2, 3, 4, 5, 6]
}
Combined list: [1, 2, 3, 4, 5, 6]
In this example,
characters
containing the characters 'a', 'b'
.additionalCharacters
containing the characters 'c', 'd'
.addAll()
method to append all elements from additionalCharacters
to the end of characters
.void main() {
List<String> characters = ['a', 'b'];
List<String> additionalCharacters = ['c', 'd'];
characters.addAll(additionalCharacters);
print('Combined list: $characters'); // Output: Combined list: [a, b, c, d]
}
Combined list: [a, b, c, d]
In this example,
fruits
containing the string 'apple'
.additionalFruits
containing the strings 'banana', 'cherry'
.addAll()
method to append all elements from additionalFruits
to the end of fruits
.void main() {
List<String> fruits = ['apple'];
List<String> additionalFruits = ['banana', 'cherry'];
fruits.addAll(additionalFruits);
print('Combined list: $fruits'); // Output: Combined list: [apple, banana, cherry]
}
Combined list: [apple, banana, cherry]
In this Dart tutorial, we learned about addAll() method of List: the syntax and few working examples with output and detailed explanation for each example.