putOrFind<S> method

S putOrFind<S>(
  1. InstanceBuilderCallback<S> dep, {
  2. String? tag,
})

Finds an existing registered instance of type S, or creates and registers a new one using dep if not already registered.

  • tag Optional tag to identify the instance.

Implementation

S putOrFind<S>(InstanceBuilderCallback<S> dep, {String? tag}) {
  final key = _getKey(S, tag);

  if (_singl.containsKey(key)) {
    final existing = _singl[key];
    if (existing == null) {
      return put(dep(), tag: tag);
    }
    return existing.getDependency() as S;
  } else {
    return put(dep(), tag: tag);
  }
}