The syntax of List.single property is:
E single
This single property of List checks that this iterable has only one element, and returns that element.
List.single returns value of type E
.
In this example,
numbers
containing the integer 1
.single
property to get the single element in the list.singleNumber
.void main() {
List<int> numbers = [1];
int singleNumber = numbers.single;
print('Single number: $singleNumber'); // Output: Single number: 1
}
Single number: 1
In this example,
characters
containing the character 'a'
.single
property to get the single element in the list.singleCharacter
.void main() {
List<String> characters = ['a'];
String singleCharacter = characters.single;
print('Single character: $singleCharacter'); // Output: Single character: a
}
Single character: a
In this example,
strings
containing the string 'apple'
.single
property to get the single element in the list.singleString
.void main() {
List<String> strings = ['apple'];
String singleString = strings.single;
print('Single string: $singleString'); // Output: Single string: apple
}
Single string: apple
In this Dart tutorial, we learned about single property of List: the syntax and few working examples with output and detailed explanation for each example.