Dart Tutorials

Dart List last
Syntax & Examples

Syntax of List.last

The syntax of List.last property is:

 

This last property of List the last element.

Return Type

List.last returns value of type .



✐ Examples

1 Get the last number in the list

In this example,

  1. We create a list named numbers containing the integers [1, 2, 3].
  2. We use the last property to retrieve the last element of the list.
  3. The last number is stored in lastNumber.
  4. We print the last number to standard output.

Dart Program

void main() {
  List<int> numbers = [1, 2, 3];
  int lastNumber = numbers.last;
  print('Last number: $lastNumber'); // Output: Last number: 3
}

Output

Last number: 3

2 Get the last character in the list

In this example,

  1. We create a list named characters containing the characters ['a', 'b', 'c'].
  2. We use the last property to retrieve the last element of the list.
  3. The last character is stored in lastCharacter.
  4. We print the last character to standard output.

Dart Program

void main() {
  List<String> characters = ['a', 'b', 'c'];
  String lastCharacter = characters.last;
  print('Last character: $lastCharacter'); // Output: Last character: c
}

Output

Last character: c

3 Get the last string in the list

In this example,

  1. We create a list named strings containing the strings ['apple', 'banana', 'cherry'].
  2. We use the last property to retrieve the last element of the list.
  3. The last string is stored in lastString.
  4. We print the last string to standard output.

Dart Program

void main() {
  List<String> strings = ['apple', 'banana', 'cherry'];
  String lastString = strings.last;
  print('Last string: $lastString'); // Output: Last string: cherry
}

Output

Last string: cherry

Summary

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