removeLast method
Removes and returns the last object in this list.
The list must be growable and non-empty.
final parts = <String>['head', 'shoulder', 'knees', 'toes'];
final retVal = parts.removeLast(); // toes
print(parts); // [head, shoulder, knees]
Implementation
@override
E? removeLast() {
_checkUnmodifiableAndFixLength();
if (length == 0) {
// purge GCed
throw IterableElementError.noElement();
}
if (_segList.isEmpty) {
_length -= 1;
return null;
}
final seg = _segList.last;
if (seg.end < _length) {
_length -= 1;
return null;
}
final elements = seg._elements;
final elmWeakRef = elements.removeLast();
_elmCount -= 1;
final elm = elmWeakRef.target;
if (elements.isEmpty) {
// all elements removed in the segment; remove the segment
_segList.removeLast();
}
_length -= 1;
return elm;
}