What is the List addAll() method in Dart?

List addAll() method

The addAll() method in Dart is used to add multiple elements to the end of a list.

Syntax

list_name.addAll(
    Iterable<E> iterable
);

list_name is the name of the list.

Parameter

  • Iterable<E> iterable: represents the elements to be added.

Note: The Iterable must have the same element type as the list. Otherwise, compile error is thrown.

Return type

void

Code

The following code shows how to use the method addAll() in dart:

void main(){
// Create List fruits
List<String> fruits = ['Mango', 'Pawpaw', 'Avocado', 'Pineapple', 'Lemon'];
// Display result
print("Original list $fruits");
// Another list
List<String> newFruits = ['Apple', 'Orange'];
// Insert new items in the list
// using addAll()
fruits.addAll(newFruits);
// Display result
print('The new list: ${fruits}');
}

If the addAll() method is applied on a null list, it will throw an error.

Unhandled exception:
 NoSuchMethodError: The method 'addAll' was called on null.

Free Resources