What is the List.removeLast() method in Dart?
The List.removeLast() method in Dart removes the last item from the list and returns it.
Syntax
List_name.removeLast();
List_nameis the name of the list.
Parameter
The removeLast() method takes no parameter.
Return value
The removeLast() method returns the last item removed from the list.
Code
The following code shows how to use the removeLast() method in Dart:
void main() {// Create the listList myList = ['Mango', 'Apple', 'Pineapple', 'Lemon'];// Display the resultsprint('The list before removing the last item: ${myList}');// Assign the items removed to variable resvar res = myList.removeLast();// Display the valueprint('The value of item removed: ${res}');// Display the resultsprint('The new list after removing the last item: ${myList}');}
Explanation
- Line 3: We create a list.
- Line 5: We display the result.
- Line 7: We assign the item removed to variable
res. - Line 9: We display the deleted value.
- Line 11: We display the result.