retainWhere method Null safety

  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.

var numbers = ['one', 'two', 'three', 'four'];
numbers.retainWhere((item) => item.length == 3);
numbers.join(', '); // 'one, two'

The list must be growable.

Implementation

@override
void retainWhere(bool Function(T element) test) {
  _context.conditionallyRunInAction(() {
    final removedElements = Queue<ElementChange<T>>();
    for (var i = _list.length - 1; i >= 0; --i) {
      final element = _list[i];
      if (!test(element)) {
        removedElements.addFirst(ElementChange(
            index: i, oldValue: element, type: OperationType.remove));
        _list.removeAt(i);
      }
    }
    if (removedElements.isNotEmpty) {
      _notifyElementsUpdate(removedElements.toList(growable: false));
    }
  }, _atom);
}