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

How to check if instance of a LazySingle is already created inside of getIt? #257

Closed
mrgzi opened this issue Feb 15, 2022 · 19 comments
Closed

Comments

@mrgzi
Copy link

mrgzi commented Feb 15, 2022

GetIt.I.registerLazySingleton(() => CrosswordManager());

I tried to check if an object was created by LazySingleton at least one time but I didn't find it. Because I want to reset the object somewhere.

@escamoteur
Copy link
Collaborator

escamoteur commented Feb 15, 2022 via email

@mrgzi
Copy link
Author

mrgzi commented Feb 15, 2022

@escamoteur because otherwise I get this error:

StateError (Bad state: This instance of the type CrosswordManager is not available in GetIt If you have registered it as LazySingleton, are you sure you have used it at least once?)

@mrgzi
Copy link
Author

mrgzi commented Feb 15, 2022

@escamoteur I need to reset those data in somewhere inside of object.

@escamoteur
Copy link
Collaborator

escamoteur commented Feb 15, 2022 via email

@mrgzi
Copy link
Author

mrgzi commented Feb 15, 2022

Yes, I used resetLazySingleton. So why this should not be possible?

@mrgzi
Copy link
Author

mrgzi commented Feb 15, 2022

@escamoteur let me explain the scenario. I registered my object as a lazySingleton. I called this object the first time on the third screen in my app but I am calling this object with a parameter. When the user returns to back the second screen and then selects the third screen, I need to reset my singleton object and create it with a different parameter.

@escamoteur
Copy link
Collaborator

escamoteur commented Feb 15, 2022 via email

@mrgzi
Copy link
Author

mrgzi commented Feb 15, 2022

@escamoteur yes it would make more sense. But I tried to use registerFactoryAsync but I got some errors and didn't understand usage. Can you add more examples to this package?

@escamoteur
Copy link
Collaborator

escamoteur commented Feb 15, 2022 via email

@mrgzi
Copy link
Author

mrgzi commented Feb 15, 2022

@escamoteur I solved some of issues myself. But I am still getting some error. For example I used registerFactory and registerFactoryAsync to create new object, but when I create new one getIt give me error like this:

ArgumentError (Invalid argument(s): Object/factory with type CrosswordManager is already registered inside GetIt.

What should I do? Maybe I misunderstand something. It would be good if there is a discord channel for this package. Or maybe more example. At least there should be some examples under the functions of descriptions.

@mrgzi
Copy link
Author

mrgzi commented Feb 15, 2022

registers a type so that a new instance will be created on each call of [get] on that type [T] type to register [factoryFunc] factory function for this type [instanceName] if you provide a value here your factory gets registered with that name instead of a type. This should only be necessary if you need to register more than one instance of one type. Its highly not recommended

I was misunderstand something. I don't need to create more than one instance of one type. I just need one instance of one type and should be able to reset existing data. I would actually do this manually but I wanted to do this in a more good way because maybe I would forget to reset some data and some bugs can appear in my app. So then I need to know if an instance was created or not.

@fullflash
Copy link

it would be nice to make publicly available method _findFactoryByNameAndType
we need to check if singleton object is created n order to use it without creating.
Anyway such method already exists but private.

@escamoteur
Copy link
Collaborator

@mgazisalik you could use a scope for that. Maby my talk https://www.youtube.com/watch?v=YJ52kSfSMyM&list=PL-BFoWYMGZ2TvwY0uf1fBJB1358utBlAz&index=10 will also help you

@escamoteur
Copy link
Collaborator

@fullflash what's wrong with

  /// Tests if an [instance] of an object or aType [T] or a name [instanceName]
  /// is registered inside GetIt
  bool isRegistered<T extends Object>({Object? instance, String? instanceName});

@fullflash
Copy link

isRegistered is checking if class is registered but we were asking for ability to check if instance is created .

isRegistered will be true after called registerLazySingleton but how to know if singleton instance is called before.

@escamoteur
Copy link
Collaborator

escamoteur commented Feb 25, 2022 via email

@mrgzi
Copy link
Author

mrgzi commented Mar 16, 2022

Can you please provide this api? I still need this. For instance, I have an object and it should be created with lazy. And I need to reset when this object should not be used. Please, I need this api. If you will not provide this api, I will use my own singleton objects and will remove get_it package.

@escamoteur
Copy link
Collaborator

As noone has brought a conclusve argument why you need this, I close this issue now. If you have to check if your lazy Singleton is already created that feels like a code smell of a bad design to me.

@escamoteur escamoteur changed the title How to check if object created inside of getIt? How to check if instance of a LazySingle is already created inside of getIt? Apr 15, 2022
@escamoteur
Copy link
Collaborator

escamoteur commented Apr 28, 2022 via email

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