The syntax of List.first property is:
This first property of List the first element.
List.first returns value of type .
In this example,
numbers
with elements [1, 2, 3, 4, 5]
.first
property.firstNumber
.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
int firstNumber = numbers.first;
print('First number: $firstNumber'); // Output: First number: 1
}
First number: 1
In this example,
characters
with elements ['a', 'b', 'c']
.first
property.firstCharacter
.void main() {
List<String> characters = ['a', 'b', 'c'];
String firstCharacter = characters.first;
print('First character: $firstCharacter'); // Output: First character: a
}
First character: a
In this example,
strings
with elements ['apple', 'banana', 'cherry']
.first
property.firstString
.void main() {
List<String> strings = ['apple', 'banana', 'cherry'];
String firstString = strings.first;
print('First string: $firstString'); // Output: First string: apple
}
First string: apple
In this Dart tutorial, we learned about first property of List: the syntax and few working examples with output and detailed explanation for each example.