Dart List removeAt()
Syntax & Examples


Syntax of List.removeAt()

The syntax of List.removeAt() method is:

 E removeAt(int index) 

This removeAt() method of List removes the object at position index from this list.

Parameters

ParameterOptional/RequiredDescription
indexrequiredthe position of the object to be removed from the list

Return Type

List.removeAt() returns value of type E.



✐ Examples

1 Remove element at index 2 from the list of numbers

In this example,

  1. We create a list named numbers containing the integers [1, 2, 3, 4, 5].
  2. We then use the removeAt() method to remove the element at index 2 (value 3) from the list.
  3. As a result, the list becomes [1, 2, 4, 5].
  4. We print the modified list to standard output.

Dart Program

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  numbers.removeAt(2); // Output: [1, 2, 4, 5]
  print(numbers);
}

Output

[1, 2, 4, 5]

2 Remove element at index 1 from the list of characters

In this example,

  1. We create a list named characters containing the characters ['a', 'b', 'c'].
  2. We then use the removeAt() method to remove the element at index 1 (value 'b') from the list.
  3. As a result, the list becomes ['a', 'c'].
  4. We print the modified list to standard output.

Dart Program

void main() {
  List<String> characters = ['a', 'b', 'c'];
  characters.removeAt(1); // Output: ['a', 'c']
  print(characters);
}

Output

[a, c]

3 Remove element at index 0 from the list of words

In this example,

  1. We create a list named words containing the strings ['apple', 'banana', 'cherry'].
  2. We then use the removeAt() method to remove the element at index 0 (value 'apple') from the list.
  3. As a result, the list becomes ['banana', 'cherry'].
  4. We print the modified list to standard output.

Dart Program

void main() {
  List<String> words = ['apple', 'banana', 'cherry'];
  words.removeAt(0); // Output: ['banana', 'cherry']
  print(words);
}

Output

[banana, cherry]

Summary

In this Dart tutorial, we learned about removeAt() method of List: the syntax and few working examples with output and detailed explanation for each example.