get<T> method

T get<T>(
  1. [String instanceName]

retrieves or creates an instance of a registered type T depending on the registration function used for this type or based on a name.


T get<T>([String instanceName]) {
    (!(const Object() is! T) && instanceName == null),
        'GetIt: You have to provide either a type or a name. Did you accidentally do  `var sl=GetIt.instance();` instead of var sl=GetIt.instance;'),

  _ServiceFactory<T> object;
  if (instanceName == null) {
    object = _factories[T];
  } else {
    final registeredObject = _factoriesByName[instanceName];
    if (registeredObject != null) {
      if (registeredObject.instance != null &&
          registeredObject.instance is! T) {
        throw ArgumentError(
            "Object with name $instanceName has a different type (${registeredObject.registrationType.toString()}) than the one that is inferred (${T.toString()}) where you call it");
    object = registeredObject;
  if (object == null && instanceName == null) {
    throw ArgumentError.value(T,
        "Object of type ${T.toString()} is not registered inside GetIt.\n Did you forget to pass an instance name? \n(Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;)");
  if (object == null && instanceName != null) {
    throw ArgumentError.value(instanceName,
        "Object with name $instanceName is not registered inside GetIt");
  return object.getObject();