Dart List last
Syntax & Examples
Syntax of List.last
The syntax of List.last property is:
This last property of List the last element.
Return Type
List.last returns value of type .
✐ Examples
1 Get the last number in the list
In this example,
- We create a list named
numbers
containing the integers[1, 2, 3]
. - We use the
last
property to retrieve the last element of the list. - The last number is stored in
lastNumber
. - We print the last number to standard output.
Dart Program
void main() {
List<int> numbers = [1, 2, 3];
int lastNumber = numbers.last;
print('Last number: $lastNumber'); // Output: Last number: 3
}
Output
Last number: 3
2 Get the last character in the list
In this example,
- We create a list named
characters
containing the characters['a', 'b', 'c']
. - We use the
last
property to retrieve the last element of the list. - The last character is stored in
lastCharacter
. - We print the last character to standard output.
Dart Program
void main() {
List<String> characters = ['a', 'b', 'c'];
String lastCharacter = characters.last;
print('Last character: $lastCharacter'); // Output: Last character: c
}
Output
Last character: c
3 Get the last string in the list
In this example,
- We create a list named
strings
containing the strings['apple', 'banana', 'cherry']
. - We use the
last
property to retrieve the last element of the list. - The last string is stored in
lastString
. - We print the last string to standard output.
Dart Program
void main() {
List<String> strings = ['apple', 'banana', 'cherry'];
String lastString = strings.last;
print('Last string: $lastString'); // Output: Last string: cherry
}
Output
Last string: cherry
Summary
In this Dart tutorial, we learned about last property of List: the syntax and few working examples with output and detailed explanation for each example.