-
-
Notifications
You must be signed in to change notification settings - Fork 153
If I only use registerLazySingleton, is there an advantage over a static instance? #88
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
Comments
Please see #87 What you are describing isn't lazy but will be instantiated immediately on App startup as its an static variable. I would especially not the async support of GetIt and that you easily switch implementations |
@escamoteur Are you sure about the instantiation part? I am pretty sure that this is not the case (see article and discussion). |
Interesting, I'm not sure if this was implemented as discussed, but I will try to find out. |
I just verified, you are correct. although I couldn't find anything in the linked article, but maybe I oversaw something. |
@escamoteur Btw, the same applies to top-level So, the following is true: final topLevelSingleton = Singleton();
// The above is a shorthand for:
void main() {
GetIt.instance.registerLazySingleton(() => Singleton());
} As I said before, there are obviously advantages that |
Yeah only that you have then many global variables and you can not switch implementations. |
@escamoteur Exactly. |
Taking a look at the Flutter framework, you will notice the following pattern for singletons:
https://github.com/flutter/flutter/blob/4d7525f05c05a6df0b29396bc9eb78c3bf1e9f89/packages/flutter/lib/src/services/raw_keyboard.dart#L493
I think that this is functionally equivalent to
get_it
'sregisterLazySingleton
asfinal
variables that are "top-level or class variable[s are] initialized the first time [they are] used".Am I correct with this assumption?
In that case, which is at least the most common use-case for me, I would not need
get_it
. Then, only the additional features are appealing (might be worth a note in the README then) 🙃The text was updated successfully, but these errors were encountered: