resolveAs<S, T extends S> method
Attemps to resolve the type S
and tries to cast it to T.
If name
is set, the instance or builder registered with this
name will be get.
This method is only available for Testing.
See also:
- KiwiContainer.resolve for resolving the object itself.
- KiwiContainer.registerFactory for register a builder function.
- KiwiContainer.registerInstance for register an instance.
Implementation
@visibleForTesting
T? resolveAs<S, T extends S>([String? name]) {
final object = resolve<S>(name);
if (!silent && !(object is T)) {
throw KiwiError(
'Failed to resolve `$S` as `$T`:\n\nThe type `$S` as `$T` was not registered${name == null ? '' : ' for the name `$name`'}\n\nMake sure `$T` is added to your KiwiContainer and rerun build_runner build\n(If you are using the kiwi_generator)\n\nWhen using Flutter, most of the time a hot restart is required to setup the KiwiContainer again.');
}
if (object == null) return null;
if (object is T) return object as T;
return null;
}