get method
Returns an instance from the injector based on the provided token
.
HeroService heroService = injector.get(HeroService);
If not found, either:
- Returns
notFoundValue
if set to a non-default value. - Throws an error (default behavior).
An injector always returns itself if Injector is given as a token.
Implementation
@mustCallSuper
dynamic get(
Object token, [
Object? notFoundValue = throwIfNotFound,
]) {
errors.debugInjectorEnter(token);
final result = provideUntyped(token, notFoundValue);
if (identical(result, throwIfNotFound)) {
throw errors.noProviderError(token);
}
errors.debugInjectorLeave(token);
return result;
}