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,
- We create a list
numberswith one element, the number 5. - We use the
singleproperty to retrieve the single element from the list. - 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,
- We create a list
characterswith one element, the character 'a'. - We use the
singleproperty to retrieve the single element from the list. - 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,
- We create a list
wordswith one element, the string 'hello'. - We use the
singleproperty to retrieve the single element from the list. - 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.