reversed property
Iterable<T>
get
reversed
Returns a new iterable with the elements of this iterable in reverse order.
This getter checks if the current iterable is a list. If it is, it directly returns the reversed list. Otherwise, it converts the iterable to a list and then returns the reversed list.
This approach avoids unnecessary list conversions if the iterable is already a list.
Returns: Iterable
Example:
List<int> numbers = [1, 2, 3, 4, 5];
Iterable<int> reversedNumbers = numbers.reversed;
print(reversedNumbers); // Output: (5, 4, 3, 2, 1)
Note: The returned iterable is lazily evaluated, meaning it reverses the elements only when they are iterated over.
Implementation
Iterable<T> get reversed {
return this is List<T> ? (this as List<T>).reversed : toList().reversed;
}