remove<T> method

int remove<T>({
  1. dynamic key,
  2. bool dispose = false,
})

Removes specific object with given key or by Type from ControlFactory. When given key is null, then key is T - check ControlFactory.keyOf for more info. If object of given key is not found, then all instances of T are removed. Set dispose to dispose removed object/s.

Control provides static call for this function via Control.remove.

Returns number of removed items.

Implementation

int remove<T>({dynamic key, bool dispose = false}) {
  key = keyOf<T>(key: key);

  assert(key != null);

  int count = 0;

  if (_items.containsKey(key)) {
    final item = _items.remove(key);
    if (dispose && item is Disposable) {
      item.dispose();
    }
    count++;
  } else if (T != dynamic) {
    _items.removeWhere((key, value) {
      final remove = value is T;

      if (remove) {
        count++;
        if (dispose && value is Disposable) {
          value.dispose();
        }
      }

      return remove;
    });
  }

  return count;
}