getOrNullK method

  1. @protected
FutureOr<Object>? getOrNullK(
  1. Entity<Object> typeEntity, {
  2. Entity<Object>? groupEntity,
  3. bool traverse = true,
})
inherited

Retrieves a dependency of the exact type typeEntity registered under the specified groupEntity.

Note that this method will not return instances of subtypes. For example, if typeEntity is Entity('List<dynamic>') and Entity('List<String>') is actually registered, this method will not return that registered dependency. This limitation arises from the use of runtime types. If you need to retrieve subtypes, consider using the standard get method that employs generics and will return subtypes.

If the dependency exists, it is returned; otherwise, null is returned.

If traverse is set to true, the search will also include all parent containers.

The return type is a FutureOr, which means it can either be a Future or a resolved value.

If the dependency is registered as a non-future, the returned value will always be non-future. If it is registered as a future, the returned value will initially be a future. Once that future completes, its resolved value is re-registered as a non-future, allowing future calls to this method to return the resolved value directly.

Implementation

@protected
FutureOr<Object>? getOrNullK(
  Entity typeEntity, {
  Entity? groupEntity,
  bool traverse = true,
}) {
  final groupEntity1 = groupEntity ?? focusGroup;
  final existingDep = _getDependencyOrNullK(
    typeEntity,
    groupEntity: groupEntity1,
    traverse: traverse,
  );
  final value = existingDep?.value;
  switch (value) {
    case Future<Object> _:
      return value.then(
        (value) {
          _registerDependencyK(
            dependency: Dependency(
              value,
              metadata: existingDep!.metadata,
            ),
            checkExisting: false,
          );
          registry.removeDependencyK(
            typeEntity,
            groupEntity: groupEntity1,
          );
          return value;
        },
      );
    case Object _:
      return value;
    default:
      return null;
  }
}