delete<T> static method

bool delete<T>({
  1. String? tag,
})

Removes a dependency from the store.

If the dependency is a ReactiveController, its onClose method is called before removal to perform cleanup.

Parameters:

  • tag: Optional identifier if multiple instances exist

Returns true if deletion was successful, false if not found.

Note: Phoenix dependencies will be recreated on next find call.

Example:

// Delete a controller
Dependency.delete<UserController>();

// Delete tagged instance
Dependency.delete<ApiClient>(tag: 'staging');

// In a widget's dispose:
@override
void dispose() {
  Dependency.delete<MyController>();
  super.dispose();
}

Implementation

static bool delete<T>({String? tag}) {
  final key = _getKey(T, tag: tag);

  if (_dependencyStore[key] == null && _lazyBuilders[key] == null) {
    Logger.info(
      'class $T is tried to delete but not present in the Dependency store',
      tag: 'Dependency',
    );
    return false;
  }

  // Call onClose if it's a ReactiveController
  if (_dependencyStore[key] != null &&
      _dependencyStore[key] is ReactiveController) {
    Logger.info('$T onClose() method called', tag: 'Dependency');
    (_dependencyStore[key] as ReactiveController).onClose();
  }

  _dependencyStore[key] = null;

  // Only remove lazy builder if NOT in fenix mode
  if (_lazyBuilders[key] != null && !_fenixKeys.contains(key)) {
    _lazyBuilders.remove(key);
  }

  Logger.info('class $T is deleted from the Dependency store',
      tag: 'Dependency');
  return true;
}