The add()
method is used to add an element at the end of the list, extending the list’s length by one.
list_name.add(
E value
);
In the code block above, list_name
is the name of the list.
value
: The element to be added to the list.This method does not return anything.
The following code snippet demonstrates how to use the add()
method in Dart.
void main(){// Create List fruitsList<String> fruits = ['Mango', 'Pawpaw', 'Avocado', 'Pineapple', 'Lemon'];// Display resultprint("Original list $fruits");// Insert new item in the list// using add()fruits.add('Apple');// Display resultprint('The new list: ${fruits}');// Add new elementfruits.add('Orange');// Display resultprint('The new list: ${fruits}');}
If the
add()
method is applied on anull
list, it will throw aNoSuchMethodError
exception.