The syntax of List.removeLast() method is:
E removeLast()
This removeLast() method of List removes and returns the last object in this list.
Parameter | Optional/Required | Description |
---|---|---|
none | none | This method takes no parameters. |
List.removeLast() returns value of type E
.
In this example,
numbers
containing the integers [1, 2, 3, 4, 5]
.removeLast()
method on numbers
to remove and retrieve the last element.void main() {
var numbers = [1, 2, 3, 4, 5];
var removed = numbers.removeLast();
print('Removed element: $removed');
print('Remaining list: $numbers');
}
Removed element: 5 Remaining list: [1, 2, 3, 4]
In this example,
characters
containing the characters ['a', 'b', 'c', 'd', 'e']
.removeLast()
method on characters
to remove and retrieve the last element.void main() {
var characters = ['a', 'b', 'c', 'd', 'e'];
var removed = characters.removeLast();
print('Removed element: $removed');
print('Remaining list: $characters');
}
Removed element: e Remaining list: [a, b, c, d]
In this example,
strings
containing the strings ['apple', 'banana', 'cherry']
.removeLast()
method on strings
to remove and retrieve the last element.void main() {
var strings = ['apple', 'banana', 'cherry'];
var removed = strings.removeLast();
print('Removed element: $removed');
print('Remaining list: $strings');
}
Removed element: cherry Remaining list: [apple, banana]
In this Dart tutorial, we learned about removeLast() method of List: the syntax and few working examples with output and detailed explanation for each example.