removeLast()
Removes and returns the last element of the queue.
The queue must not be empty when this method is called.
extension IterableExtensions<E> on Iterable<E> {
E? removeLast() {
if (isEmpty) return null;
var list = toList();
var last = list.removeLast();
return last;
}
}
void main() {
var iterable = [1, 2, 3, 4, 5];
print(iterable.removeLast()); // Output:
var removed = iterable.removeLast();
print('Removed element: $removed'); // Output: Removed element: 5
print('Remaining elements: $iterable'); // Output: Remaining elements: [1, 2, 3, 4]
}
5
Removed element: 4
Remaining elements: [1, 2, 3]
Exited.