get method

  1. @mustCallSuper
dynamic get(
  1. Object token,
  2. [Object notFoundValue = throwIfNotFound]
)
@mustCallSuper, inherited

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)) {
    return throwsNotFound(this, token);
  }
  errors.debugInjectorLeave(token);
  return result;
}