remove method Null safety

  1. @override
bool remove(
  1. Object? element
)
override

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) {
  var didRemove = false;

  _context.conditionallyRunInAction(() {
    final index = _list.indexOf(element as T);
    if (index >= 0) {
      _list.removeAt(index);
      _notifyElementUpdate(index, null, element, type: OperationType.remove);
      didRemove = true;
    }
  }, _atom);

  return didRemove;
}