getAsyncLazyLocalInstance<T extends MvvmInstance> method

Future<T> getAsyncLazyLocalInstance<T extends MvvmInstance>({
  1. int index = 0,
})

Returns connected instance of given type

index - index of instance if multiple are connected

Implementation

Future<T> getAsyncLazyLocalInstance<T extends MvvmInstance>(
    {int index = 0}) async {
  final typedInstances = _instances[T];
  final typedBuilders = _lazyInstancesBuilders[T];

  if (index < 0 || index >= typedBuilders!.length) {
    throw IllegalArgumentException(
      message:
          'The index = $index value must be non-negative and less than count of instances of $T.',
    );
  }

  if (typedInstances?[index] == null ||
      index >= (typedInstances?.length ?? 0)) {
    final object = await typedBuilders[index]();

    if (typedInstances == null) {
      _instances[T] = [object];
    } else {
      typedInstances[index] = object;
    }

    return object;
  }

  return _instances[T]![index] as T;
}