Dart Set first
Syntax & Examples


Set.first property

The `first` property in Dart returns the first element of the set.


Syntax of Set.first

The syntax of Set.first property is:

 E first 

This first property of Set returns the first element.

Return Type

Set.first returns value of type E.



✐ Examples

1 Retrieve the first number

In this example,

  1. We create a Set numbers containing integers.
  2. We access the first property to retrieve the first number from the set.
  3. We print the first number to standard output.

Dart Program

void main() {
  Set<int> numbers = {1, 2, 3, 4, 5};
  int firstNumber = numbers.first;
  print('First number in the set: $firstNumber');
}

Output

First number in the set: 1

2 Retrieve the first character

In this example,

  1. We create a Set characters containing characters.
  2. We access the first property to retrieve the first character from the set.
  3. We print the first character to standard output.

Dart Program

void main() {
  Set<String> characters = {'a', 'b', 'c', 'd', 'e'};
  String firstCharacter = characters.first;
  print('First character in the set: $firstCharacter');
}

Output

First character in the set: a

3 Retrieve the first string

In this example,

  1. We create a Set strings containing strings.
  2. We access the first property to retrieve the first string from the set.
  3. We print the first string to standard output.

Dart Program

void main() {
  Set<String> strings = {'apple', 'banana', 'cherry', 'date'};
  String firstString = strings.first;
  print('First string in the set: $firstString');
}

Output

First string in the set: apple

Summary

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