To get the last element in a Dart list, you can use the `last` property.
In this example,
numbers
with integer elements.last
property to get the last element.void main() {
List<int> numbers = [10, 20, 30, 40, 50];
int lastElement = numbers.last;
print('Last element is: $lastElement');
}
Last element is: 50
In this example,
names
with string elements.last
property to get the last element.void main() {
List<String> names = ['Alice', 'Bob', 'Charlie', 'David'];
String lastElement = names.last;
print('Last element is: $lastElement');
}
Last element is: David
In this tutorial, we learned How to get Last Element in a List in Dart language with well detailed examples.