Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
addall
list
communitycreator

What is the List addAll() method in Dart?

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

dart
addall
list
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring