find<T> static method

Kind<T> find<T>({
  1. T? instance,
})

Finds a registered kind for T.

If instance implements HasKind, the method tries to determine the kind with HasKind.runtimeKind.

If no kind has been found and instance is non-null, the method seeks the best kind for which Kind.isInstance is true.

If no kind has been found, the method tries to find a kind that has exactly the same type as T (Kind.dartType).

If no kind was found, the method inspects whether T is a list type of one of the registered kinds in Kind.all.

If no kind was found, the method inspects whether T is a set type of one of the registered kinds in Kind.all.

Whenever T is nullable, the method returns a NullableKind.

Throws ArgumentError if no kind is registered for T.

Implementation

static Kind<T> find<T>({T? instance}) {
  if (instance == null) {
    final kind = maybeFindByType<T>();
    if (kind != null) {
      return kind;
    }
    throw ArgumentError(
      'Could not find kind for $T.\n'
      '\n'
      'Registered kinds are: ${_kinds.map((e) => e.dartType).join(', ')}',
    );
  } else {
    final kind = maybeFindByInstance<T>(instance);
    if (kind != null) {
      return kind;
    }
    throw ArgumentError(
      'Could not find kind for $T (given an instance of ${instance.runtimeType}).\n'
      '\n'
      'Registered kinds are: ${_kinds.map((e) => e.dartType).join(', ')}',
    );
  }
}