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
Parameter | Optional/Required | Description |
---|---|---|
index | required | the 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,
- We create a list named
numbers
containing the integers[1, 2, 3, 4, 5]
. - We then use the
removeAt()
method to remove the element at index 2 (value 3) from the list. - As a result, the list becomes
[1, 2, 4, 5]
. - 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,
- We create a list named
characters
containing the characters['a', 'b', 'c']
. - We then use the
removeAt()
method to remove the element at index 1 (value 'b') from the list. - As a result, the list becomes
['a', 'c']
. - 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,
- We create a list named
words
containing the strings['apple', 'banana', 'cherry']
. - We then use the
removeAt()
method to remove the element at index 0 (value 'apple') from the list. - As a result, the list becomes
['banana', 'cherry']
. - 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.