Skip to content
New issue

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

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

Already on GitHub? # to your account

Return the instance when calling registerSingleton #242

Conversation

Rexios80
Copy link
Contributor

Just a convenience feature so you can write code like this:

final instance = GetIt.I.registerSingleton(Object());

@escamoteur
Copy link
Collaborator

Hi, sorry for not looking into this earlier, but I had some mental health problems the last half year.

Why do you think this is better than

final instance = Object();

GetIt.I.registerSingleton(instance);

@Rexios80
Copy link
Contributor Author

Rexios80 commented Feb 3, 2022

It's mostly for convenience, but it also lets you register a singleton like this:

class Class {
    final instance = GetIt.I.registerSingleton(Object());
}

Which isn't possible otherwise

@escamoteur
Copy link
Collaborator

escamoteur commented Feb 3, 2022 via email

@Rexios80
Copy link
Contributor Author

For myself I use it for more convenient initialization of the singleton:

final instance = GetIt.I.registerSingleton(Object());
instance.setup();
instance.doOtherStuff();

Not a huge deal to split the first line into two, but again this is just for convenience.

@escamoteur
Copy link
Collaborator

@esDotDev What do you think?

@esDotDev
Copy link
Collaborator

I think darts chaining ability makes this a bit redundant.

final instance = Object()..setup()..doOtherStuff();
GetIt.I.registerSingleton(instance);

On the other hand, it cant really hurt, and is a small quality of life thing, so why not?

@escamoteur
Copy link
Collaborator

What I don't like is that it would break the format of the other registerFunctions.

@Rexios80
Copy link
Contributor Author

Couldn't we make those do the same thing?

@escamoteur
Copy link
Collaborator

Unfortunately no, because all the registerFuncions that take a factoryfuncion return without creating an instance

@esDotDev
Copy link
Collaborator

I think if it's lazy, it's obvious why it wouldn't return anything. Seems ok to me.

@esDotDev
Copy link
Collaborator

If we did want to have similar functionality lazily, an onCreated: (instance){ } method could be nice?

@Rexios80
Copy link
Contributor Author

Rexios80 commented Mar 9, 2022

Just a random thought: you can't await chained async calls

@escamoteur
Copy link
Collaborator

I know this is a pretty old PR but I had to make a break over the last year

@escamoteur escamoteur merged commit ccc8e13 into fluttercommunity:master May 4, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants