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_name is 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 list
List myList = ['Mango', 'Apple', 'Pineapple', 'Lemon'];
// Display the results
print('The list before removing the last item: ${myList}');
// Assign the items removed to variable res
var res = myList.removeLast();
// Display the value
print('The value of item removed: ${res}');
// Display the results
print('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.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources