Dart List reversed
Syntax & Examples
Syntax of List.reversed
The syntax of List.reversed property is:
Iterable<E> reversed
This reversed property of List an Iterable
of the objects in this list in reverse order.
Return Type
List.reversed returns value of type Iterable<E>
.
✐ Examples
1 Get the reversed numbers in the list
In this example,
- We create a list named
numbers
containing the integers[1, 2, 3]
. - We use the
reversed
property to get an iterable of the numbers in reverse order. - The reversed numbers are stored in
reversedNumbers
. - We print the reversed numbers to standard output.
Dart Program
void main() {
List<int> numbers = [1, 2, 3];
Iterable<int> reversedNumbers = numbers.reversed;
print('Reversed numbers: $reversedNumbers'); // Output: Reversed numbers: (3, 2, 1)
}
Output
Reversed numbers: (3, 2, 1)
2 Get the reversed characters in the list
In this example,
- We create a list named
characters
containing the characters['a', 'b', 'c']
. - We use the
reversed
property to get an iterable of the characters in reverse order. - The reversed characters are stored in
reversedCharacters
. - We print the reversed characters to standard output.
Dart Program
void main() {
List<String> characters = ['a', 'b', 'c'];
Iterable<String> reversedCharacters = characters.reversed;
print('Reversed characters: $reversedCharacters'); // Output: Reversed characters: (c, b, a)
}
Output
Reversed characters: (c, b, a)
3 Get the reversed strings in the list
In this example,
- We create a list named
strings
containing the strings['apple', 'banana', 'cherry']
. - We use the
reversed
property to get an iterable of the strings in reverse order. - The reversed strings are stored in
reversedStrings
. - We print the reversed strings to standard output.
Dart Program
void main() {
List<String> strings = ['apple', 'banana', 'cherry'];
Iterable<String> reversedStrings = strings.reversed;
print('Reversed strings: $reversedStrings'); // Output: Reversed strings: (cherry, banana, apple)
}
Output
Reversed strings: (cherry, banana, apple)
Summary
In this Dart tutorial, we learned about reversed property of List: the syntax and few working examples with output and detailed explanation for each example.