get<T> method

T get<T>({
  1. String? key,
  2. Map<String, dynamic>? additionalParameters,
})

Gets an instance of the given type of T and optional given key and parameters.

Throws an InjectorException if the given type has not been mapped using the map method.

Note that instance that are mapped to need additional parameters cannot be singletons

final injector = Injector.getInstance();
// map the type
injector.map<Logger>((injector) => AppLogger());
// get the type
injector.get<Logger>().log("some message");

injector.mapWithParams<SomeType>((i, p) => SomeType(p["id"]))
final instance = injector.get<SomeType>(additionalParameters: { "id": "some-id" });
print(instance.id) // prints 'some-id'

Implementation

T get<T>({String? key, Map<String, dynamic>? additionalParameters}) {
  final objectKey = _makeKey(T, key);
  final objectFactory = _factories[objectKey];
  if (objectFactory == null) {
    throw InjectorException("Cannot find object factory for '$objectKey'");
  }

  return objectFactory.get(this, additionalParameters);
}