reload<S> method

void reload<S>({
  1. String? tag,
  2. String? key,
  3. bool force = false,
})

Reloads the instance of type S.

If tag is provided, the instance associated with that tag is reloaded. If key is provided, it will be used as the identifier for the instance to reload. If force is set to true, the instance will be reloaded even if it is marked as permanent.

This method is used to restart an instance, clearing its existing state and recreating it.

Example:

// Reloads the instance of type `MyController`.
Get.reload<MyController>();

// Reloads the instance associated with the tag 'myTag'.
Get.reload<MyService>(tag: 'myTag');

Implementation

void reload<S>({
  String? tag,
  String? key,
  bool force = false,
}) {
  final newKey = key ?? _getKey(S, tag);

  final builder = _getDependency<S>(tag: tag, key: newKey);
  if (builder == null) return;

  if (builder.permanent && !force) {
    Get.log(
      '''Instance "$newKey" is permanent. Use [force = true] to force the restart.''',
      isError: true,
    );
    return;
  }

  final i = builder.dependency;

  if (i is GetxServiceMixin && !force) {
    return;
  }

  if (i is GetLifeCycleMixin) {
    i.onDelete();
    Get.log('"$newKey" onDelete() called');
  }

  builder.dependency = null;
  builder.isInit = false;
  Get.log('Instance "$newKey" was restarted.');
}