Dart Set last
Syntax & Examples


Set.last property

The `last` property in Dart returns the last element in the set.


Syntax of Set.last

The syntax of Set.last property is:

 E last 

This last property of Set returns the last element.

Return Type

Set.last returns value of type E.



✐ Examples

1 Set of Numbers

In this example,

  1. We create a Set numbers containing some numbers.
  2. We retrieve the last element using the last property.
  3. We print the last element to standard output.

Dart Program

void main() {
  Set<int> numbers = {1, 2, 3};
  int lastElement = numbers.last;
  print('Last element in the set: $lastElement');
}

Output

Last element in the set: 3

2 Set of Characters

In this example,

  1. We create a Set characters containing some characters.
  2. We retrieve the last element using the last property.
  3. We print the last element to standard output.

Dart Program

void main() {
  Set<String> characters = {'a', 'b', 'c'};
  String lastElement = characters.last;
  print('Last element in the set: $lastElement');
}

Output

Last element in the set: c

3 Set of Strings

In this example,

  1. We create a Set strings containing some strings.
  2. We retrieve the last element using the last property.
  3. We print the last element to standard output.

Dart Program

void main() {
  Set<String> strings = {'apple', 'banana', 'orange'};
  String lastElement = strings.last;
  print('Last element in the set: $lastElement');
}

Output

Last element in the set: orange

Summary

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