get<T extends IRequestInterface> method

T get<T extends IRequestInterface>([
  1. String? key
])

Gets an initialized interface.

key parameter is optional. However, getting result by specifing key is faster.

All custom interfaces must inherit from IRequestInterface interface.

Calling this method without initializing the custom interface using initCustomInterface<T>(...) will result in InterfaceNotInitializedException

Example usage:

await client.getCustomInterface<MyCustomInterface>().create((p1) => p1.build());

Implementation

T get<T extends IRequestInterface>([String? key]) {
  if (!isReady) {
    throw ClientNotReadyException();
  }

  final interfaceKey = InterfaceKey<T>(key);

  if (exists<T>(key)) {
    return _interfaces[interfaceKey] as T;
  }

  final interfacesOfType = _interfaces.values.whereType<T>();

  if (interfacesOfType.isEmpty) {
    throw InterfaceDoNotExistException(
      'The specified interface do not exist. (${typeOf<T>()}_$key)',
    );
  }

  final interface = interfacesOfType.first;

  if (!interface._hasInitilizedAlready) {
    throw InterfaceNotInitializedException<T>();
  }

  return interface;
}