Skip to content

Get with type #298

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
lacopiroty opened this issue Oct 24, 2022 · 8 comments
Closed

Get with type #298

lacopiroty opened this issue Oct 24, 2022 · 8 comments

Comments

@lacopiroty
Copy link
Contributor

Hi! :)

Is it possible to add support for type parameter to "get/call" etc. functions?
I also created a PR :)

example for get function:

Current implementation:

T get<T extends Object>({
   String? instanceName,
   dynamic param1,
   dynamic param2,
 }) {
   final instanceFactory = _findFactoryByNameAndType<T>(instanceName);
   // normal implementation of GetIt
}

Expected implementation:

T get<T extends Object>({
    String? instanceName,
    dynamic param1,
    dynamic param2,
    Type? type,
  }) {
    final instanceFactory = _findFactoryByNameAndType<T>(instanceName, type);
    // normal implementation of GetIt
}
@shtse8
Copy link

shtse8 commented Mar 22, 2023

I have the same question, how can we resolve instance by runtime type?

@wim07101993
Copy link

This issue has been open for about 6 months, could someone please take a look at the pr? :)

@escamoteur
Copy link
Collaborator

@esDotDev @dancamdev what are your thoughts on this? get_it used the generics from the beginning and I'm somewhat hesitant to add a type, although internally I do use is too.

@esDotDev
Copy link
Collaborator

I'm not clear on the use cases, but if there are valid ones then I don't see how it would hurt to allow an optional type.

@escamoteur
Copy link
Collaborator

Some people want to access objects at runtime with a variable type

@escamoteur
Copy link
Collaborator

One question I have is, how can this be done in a typesafe way? I mean how can the analyzer control if the destination variable has the correct type?

@escamoteur
Copy link
Collaborator

included in V7.6.0

@escamoteur
Copy link
Collaborator

I just stumbled again upon these lines

    assert(
        type == null || type is T,
        'The type you passed is not a $T. This can happen '
        'if the receiving variable is of the wrong type, or you passed a gerenic type and a type parameter');

and I really wonder how any of you uses the option of passing a runtime type to get an object from get_it as it turns out this check will always fail if the receiving variable is defined with any other type than Object due to the limitation and also my misunderstanding of what the is operator it capable of see dart-lang/sdk#43390 (comment)

You should all have gotten a lot of assertion errors from that.
I now fixed that so that the check is done correctly

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

5 participants