Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What is the List insertAll() 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.

Overview

The insertAll() method in Dart is used to insert elements at a specific index in the list, which shifts the following elements by the number of the elements to be inserted.

Syntax

void insertAll(
   int index,
   Iterable<E> element
);

Parameters

  • index: the position where the element will be inserted.
  • Iterable<E> element: the elements to be inserted.

Note: The index value must be valid. The valid index ranges from 0…N, where N is the number of elements in the list.

Return value

The return type for this function is void.

Code

The following code shows how to use the insertAll() method 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 insertAll()
fruits.insertAll(2, newFruits);
// Display result
print('The new list: ${fruits}');
}

The list’s length is increased by the number of elements to be inserted.

Code explanation

We create two lists named fruits and newFruits. We use the insertAll() method to insert all of the elements in newfruits in fruits, then display the result.

Note: Passing an invalid index will throw the RangeError exception.

RELATED TAGS

dart
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