Skip to content

How to get the existing instance in case object is registered as Factory? #60

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
trietbui85 opened this issue Mar 27, 2020 · 1 comment

Comments

@trietbui85
Copy link

First of all, thank you for this great service locator library. I'm using it in my all 3 projects.

Assuming I have 2 screen A and B. I register a BaseBloc as Factory, thus each time enter these screens, I will get a new instance of BaseBloc.

Screen A has 2 child screens A1 and A2. Instead of passing BaseBloc to A1 and A2, I would like to get use GetIt to get it in A1 and A2 instead. However, since BaseBloc was registered as Factory, in A1 when I have BaseBloc, its data was reset to new instance (not have any data which from A screen).

I wonder if we have any existing solutions for this case? If not, I'm thinking about a new param for GetIt.I<BaseBloc>(returnInstanceIfExisting: true) - which will return existing instance of BaseBloc instead of creating the new one.

How do you think about my idea?

More info:

get_it: ^4.0.0

Flutter version 1.12.13+hotfix.5 at C:\DevTools\flutter
Framework revision 27321ebbad (4 months ago), 2019-12-10 18:15:01 -0800
Engine revision 2994f7e1e6
Dart version 2.7.0
@trietbui85 trietbui85 changed the title Add a param to force get the singleton or factory. How to get the existing instance in case object is registered as Factory? Mar 27, 2020
@escamoteur
Copy link
Collaborator

I wonder if we have any existing solutions for this case? If not, I'm thinking about a new param for GetIt.I(returnInstanceIfExisting: true) - which will return existing instance of BaseBloc instead of creating the new one.

is not possible because I don't keep track of factory created objects. How would I know when it is no longer needed?

I think what you would need were scoped singletons but that's something that's not supported yet.

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

2 participants