Dart .insert()
Anonymous contributor
Published Sep 21, 2024
Contribute to Docs
The .insert() method in Dart inserts an element at a specified index in a list and shifts subsequent elements to the right.
Syntax
list.insert(int index, E element);
index: The position where the element is to be inserted.element: The element is the value to be added.
Example
In the following example, the .insert() method is used to insert the number 3 at the second index:
void main() {List<int> numbers = [1, 2, 4, 5];print('Original list: $numbers');// Inserting the number 3 at index 2numbers.insert(2, 3);print('List after insertion: $numbers');}
The output of the above example code is as follows:
Original list: [1, 2, 4, 5]List after insertion: [1, 2, 3, 4, 5]
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Dart on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours