retainWhere method

  1. @override
void retainWhere(
  1. bool test(
    1. T element
    )
)
override

Removes all objects from this list that fail to satisfy test. An object o satisfies test if test(o) is true. The slice's range shrinks by the number of elements removed. Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.

Implementation

@override
void retainWhere(bool Function(T element) test) {
  int index = 0;
  int numRemoved = 0;
  _list.retainWhere((element) {
    if (index < _start || index >= _end) {
      index++;
      return true;
    }
    index++;
    if (!test(element)) {
      numRemoved++;
      return false;
    }
    return true;
  });
  _end -= numRemoved;
}