Dart Tutorials

Dart List addAll()
Syntax & Examples

Syntax of List.addAll()

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.

Parameters

ParameterOptional/RequiredDescription
iterablerequiredthe iterable whose elements will be added to the list

Return Type

List.addAll() returns value of type void.



✐ Examples

1 Combine two lists of numbers

In this example,

  1. We create a list named numbers containing the integers 1, 2, 3.
  2. We create another list named additionalNumbers containing the integers 4, 5, 6.
  3. We use the addAll() method to append all elements from additionalNumbers to the end of numbers.
  4. The combined list is printed to standard output.

Dart Program

void main() {
  List&lt;int&gt; numbers = [1, 2, 3];
  List&lt;int&gt; additionalNumbers = [4, 5, 6];
  numbers.addAll(additionalNumbers);
  print('Combined list: $numbers'); // Output: Combined list: [1, 2, 3, 4, 5, 6]
}

Output

Combined list: [1, 2, 3, 4, 5, 6]

2 Combine two lists of characters

In this example,

  1. We create a list named characters containing the characters 'a', 'b'.
  2. We create another list named additionalCharacters containing the characters 'c', 'd'.
  3. We use the addAll() method to append all elements from additionalCharacters to the end of characters.
  4. The combined list is printed to standard output.

Dart Program

void main() {
  List&lt;String&gt; characters = ['a', 'b'];
  List&lt;String&gt; additionalCharacters = ['c', 'd'];
  characters.addAll(additionalCharacters);
  print('Combined list: $characters'); // Output: Combined list: [a, b, c, d]
}

Output

Combined list: [a, b, c, d]

3 Combine two lists of strings

In this example,

  1. We create a list named fruits containing the string 'apple'.
  2. We create another list named additionalFruits containing the strings 'banana', 'cherry'.
  3. We use the addAll() method to append all elements from additionalFruits to the end of fruits.
  4. The combined list is printed to standard output.

Dart Program

void main() {
  List&lt;String&gt; fruits = ['apple'];
  List&lt;String&gt; additionalFruits = ['banana', 'cherry'];
  fruits.addAll(additionalFruits);
  print('Combined list: $fruits'); // Output: Combined list: [apple, banana, cherry]
}

Output

Combined list: [apple, banana, cherry]

Summary

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.