removeObjectInScope method

void removeObjectInScope({
  1. required String type,
  2. required String scopeId,
  3. int? index,
  4. required void onRemove(
    1. T
    ),
})

Removes object in given scope

Implementation

void removeObjectInScope({
  required String type,
  required String scopeId,
  int? index,
  required void Function(T) onRemove,
}) {
  final scopedInstances = _instances[scopeId];

  if (index == null) {
    final typeInstances = scopedInstances?[type];

    typeInstances?.forEach((value) {
      onRemove(value);
    });

    scopedInstances?.remove(type);
  } else {
    final objects = _instances[scopeId]?[type] ?? [];

    if (objects.isEmpty) {
      return;
    }

    if (index < 0 || index >= objects.length) {
      throw IllegalArgumentException(
        message:
            'The index = $index value must be non-negative and less than count of references of $type in $scopeId.',
      );
    }

    onRemove(objects[index]);

    objects.removeAt(index);

    if (objects.isEmpty) {
      scopedInstances?.remove(type);
    }
  }

  if (scopedInstances?.isEmpty ?? true) {
    _instances.remove(scopeId);
  }
}