Skip to content

Retrieval by instance name does not work #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dalewking opened this issue Dec 8, 2019 · 5 comments
Closed

Retrieval by instance name does not work #35

dalewking opened this issue Dec 8, 2019 · 5 comments

Comments

@dalewking
Copy link

dalewking commented Dec 8, 2019

Consider this simple example:

void main() {
  try {
    var sl = GetIt.instance;

    sl.registerSingleton("some String", instanceName: "foo");

    final x = sl<String>("foo");
  } catch (e) {
    print(e);
  }
}

This fails with this output:

String
Invalid argument(s): Object with name foo has a different type (String) than the one that is inferred (String) where you call it

I think the problem is this code:

    if (registeredObject.registrationType is! T) {
      print(T.toString());
      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");
    }

registrationType has a type of Type so this code is asking whether an instance of Type has the type String and the answer is of course no. I think you want to replace is! with !=.

@dalewking
Copy link
Author

Alternatively you might want to go ahead and get the object from the factory then see if the object returned from the factory is T which would let you retrieve a named instance using a super class type as in:

sl.registerSingleton(someSubclassInstance, instanceName: "foo");

SuperClassType x = sl("foo");

@knaeckeKami
Copy link
Contributor

knaeckeKami commented Dec 13, 2019

Yep, this is already fixed in master, see:
#32

As a workaround until a new release is shipped, you can just replace the type parameters with <dynamic> when using named instances.

@dalewking
Copy link
Author

@knaeckeKami Unfortunately, I think markdown syntax swallowed what you were trying to say.

@knaeckeKami
Copy link
Contributor

My bad, edited.

@escamoteur
Copy link
Collaborator

Should be fixed with new version V3.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants