Dart Runes single
Syntax & Examples

Runes.single property

The `single` property in Dart checks that this iterable has only one element and returns that element.


Syntax of Runes.single

The syntax of Runes.single property is:

 int single 

This single property of Runes checks that this iterable has only one element, and returns that element.

Return Type

Runes.single returns value of type int.



✐ Examples

1 Using `single` with a list of numbers

In this example,

  1. We create a list numbers with one element, the number 5.
  2. We use the single property to retrieve the single element from the list.
  3. We then print the element to standard output.

Dart Program

void main() {
  List<int> numbers = [5];
  int singleNumber = numbers.single;
  print(singleNumber);
}

Output

5

2 Using `single` with a list of characters

In this example,

  1. We create a list characters with one element, the character 'a'.
  2. We use the single property to retrieve the single element from the list.
  3. We then print the element to standard output.

Dart Program

void main() {
  List<String> characters = ['a'];
  String singleChar = characters.single;
  print(singleChar);
}

Output

a

3 Using `single` with a list of strings

In this example,

  1. We create a list words with one element, the string 'hello'.
  2. We use the single property to retrieve the single element from the list.
  3. We then print the element to standard output.

Dart Program

void main() {
  List<String> words = ['hello'];
  String singleWord = words.single;
  print(singleWord);
}

Output

hello

Summary

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