Skip to content
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

[Dart 3.0] GetIt can't detect T type properly #331

Open
subzero911 opened this issue May 26, 2023 · 6 comments
Open

[Dart 3.0] GetIt can't detect T type properly #331

subzero911 opened this issue May 26, 2023 · 6 comments

Comments

@subzero911
Copy link

After the latest Flutter upgrade, registerSingleton inside of VoidCallback is not working anymore:

image

It worked before Dart 3.0. Now they probably have stricter type checking.
In runtime it crashes with the following error
image

The bug disappears when I explicitly indicate the type:
image

This bug appears in VoidCallback only, not in a plain code. It also can be fixed if I used:
() {} instead of () =>

image

The reason behind: a generic by default takes the root return type (void), and Dart tries to put void into T extends Object constraint, and this apparently causes this assert to be falsely posivite:
image

Any ideas on it?

@escamoteur
Copy link
Collaborator

@subzero911 your analysis seems correct, although I don't think that this can be fixed by GetIt. I always recommend passing the Generic type explicitly because then there is no room for doubt

@mraleph @munificent this comes sort of surprising, is that the intended consequence?

@subzero911
Copy link
Author

First of all, it's strange that we can do call GetIt.instance(). Is it a callable class? I personally never did it. Do we actually need this assertion?

@escamoteur
Copy link
Collaborator

escamoteur commented May 27, 2023

Actually the assertion isn't about calling it this way, but if people call GetIt.instance() accidentially you get this type of eception too.
And yes GetIt is a callable class from the beginning.
I most of the time to something like this in my setup file:

final di = GetIt.instance;
// and then use it like
di<Mytype>().myMethod();

@subzero911
Copy link
Author

I always recommend passing the Generic type explicitly because then there is no room for doubt

But writing GetIt.I.registerSingleton<MyControllerWithVeryLongName>(MyControllerWithVeryLongName()) every time is not concise.

@escamoteur
Copy link
Collaborator

escamoteur commented May 29, 2023 via email

@munificent
Copy link

I'm not sure why this behavior is different in Dart 3.0. @stereotype441 or @eernst might know more.

flawnn added a commit to flawnn/injectable that referenced this issue Mar 12, 2024
This fixes the now (from Dart SDK 3 onwards required) generic type annotation when calling `registerSingleton` from GetIt. Check the following issue: 
fluttercommunity/get_it#331 (comment)
flawnn added a commit to flawnn/injectable that referenced this issue Mar 12, 2024
This fixes the now (from Dart SDK 3 onwards required) generic type annotation when calling `registerSingleton` from GetIt. Check the following issue: 
fluttercommunity/get_it#331 (comment)
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