addAll()
methodThe addAll()
method in Dart is used to add multiple elements to the end of a list.
list_name.addAll(
Iterable<E> iterable
);
list_name
is the name of the list.
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.
void
The following code shows how to use the method addAll()
in dart:
void main(){// Create List fruitsList<String> fruits = ['Mango', 'Pawpaw', 'Avocado', 'Pineapple', 'Lemon'];// Display resultprint("Original list $fruits");// Another listList<String> newFruits = ['Apple', 'Orange'];// Insert new items in the list// using addAll()fruits.addAll(newFruits);// Display resultprint('The new list: ${fruits}');}
If the
addAll()
method is applied on anull
list, it will throw an error.
Unhandled exception:
NoSuchMethodError: The method 'addAll' was called on null.