remove method
Removes the first occurrence of value from this list.
Returns true if value was in the list, false otherwise.
The list must be growable.
final parts = <String>['head', 'shoulders', 'knees', 'toes'];
final retVal = parts.remove('head'); // true
print(parts); // [shoulders, knees, toes]
The method has no effect if value was not in the list.
final parts = <String>['shoulders', 'knees', 'toes'];
// Note: 'head' has already been removed.
final retVal = parts.remove('head'); // false
print(parts); // [shoulders, knees, toes]
Implementation
@override
bool remove(Object? element) {
_checkUnmodifiableAndFixLength();
if (length == 0) return false; // purge GCed
if (element == null) {
if (_segList.isEmpty) {
_length -= 1;
return true;
}
// remove the first null element
final seg = _segList.first;
if (seg.start > 0) {
// before first
_shift(0, -1);
return true;
} else if (seg.end < _length) {
// after first
_shift(1, -1);
return true;
}
return false;
}
// element != null
final len = _segList.length;
for (int j = 0; j < len; ++j) {
int k = 0;
final seg0 = _segList[j];
final elements = seg0._elements;
for (final elm in elements) {
if (elm.target == element) {
// found
elements.removeAt(k);
_elmCount -= 1;
if (elements.isEmpty) {
// total removed; remove this segment
_segList.removeAt(j);
}
_shift(j + 1, -1);
return true;
}
k += 1;
}
}
return false;
}