-
-
Notifications
You must be signed in to change notification settings - Fork 149
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
Why do we need get_it? #188
Comments
Let me put it this way, you are not completely wrong. You definitely can use just global functions and global variables to make state accessible to your UI. Another aspect is scoping of the objects. Inherited widgets as well as get_it allow you to override registered objects based on a current scope. For inherited widgets this scope is defined by your current position in the widget tree, in get_it you can push and pop registrations scopes independent of the widget tree. Scopes allow you to override existing behaviour or to easily manage the lifetime and disposal of objects. The general idea of any dependency injection system is that you have defined point in your code where you have all your setup and configuration. You wrote your already using some sort of state management solution. Which probably means that the solution already offer some sort of object location. In this case you probably won't need get_it. I hope this could clarify your question a bit. |
@escamoteur Thank you! This clarifies it a lot. Lastly this doesn't affect memory, right? |
yeah that is right. But by using scopes e.g. it's easier to make sure that objects that you no longer need are freed. If they are global variables they never will be freed unless you assign null to them. |
Awesome, thanks a ton! |
Sorry for my grammar :) |
@ntaworking If you register them with However unless your classes create thousands of objects when created or load 100 images, you rarely should worry about the memory that your own classes consume. Our current phones have more than enough. It's a different thing if the creation of these classes takes a lot of time then it can make sense to use a lazy Singleton to speed up startup time. But in such cases it's often better to use an async registration and await the finish of all initialization using |
I do not understand the benefits of using get_it or InheritedWidget.
I've looked into why we need InheritedWidget, this solves the data passing problem. However for that we have a state management system so we do not need InheritedWidget at all.
I've looked into get_it and from my understanding if we are already using a state management system the only benefit we would have is the ability to encapsulate the services/methods related to a chunk of widgets into one place. (dependency injection)
For example if we have a map and a locate me button then they could share the same
_locateMe
service.For this we would create an abstract class that defines the _locateMe method and connect it with the dependency injection using a
locator.registerLazySingleton
.But what is the point? I can just create a
methods.dart
file with thelocateMe
method without any classes, we can just put the method into themethods.dart
which is faster and easier and we can access it from anywhere.I am not sure how dart internally works, what makes sense for me is that
registerLazySingleton
would remove the _locateMe method from memory after I use the_locateMe
method. And if we put thelocateMe
method inside a normal.dart
file without classes or anything else it will be always in memory hence less performant.Is my assumption true? Is there something I am missing?
The text was updated successfully, but these errors were encountered: