What is List.removeAt() in Dart?

Overview

The removeAt() method removes the value at the specified index in the list.

Syntax

The syntax of the function is as follows:

List_name.removeAt(int index);

Note: List_name is the name of the list.

Parameters

  • Index (required): The index of the element that should be removed from the list.

Return value

The removeAt() method returns the element that is removed from the list.

Code

The following code demonstrates the use of list.removeAt() in Dart.

void main() {
//Creating list
var myList = [2,6, 'Apple', 'Pen', 'three', 'Mango', 'five'];
// display result
print('The list before removing item from the list: ${myList}');
// Remove item using removeAt()
var res = myList.removeAt(3);
// Print result
print('The Item removed: ${res}');
print('The new list after removing the list element ${myList}');
}

Explanation

  • Line 3: We create a list called myList with seven elements.

  • Line 8: We use the removeAt() method to remove an item from the index position 3.

  • Lines 10 and 12: We display the result.

We can see that pen, the element at index position 3, is removed.

Free Resources