fluttercommunity / get_it

Get It - Simple direct Service Locator that allows to decouple the interface from a concrete implementation and to access the concrete implementation from everywhere in your App. Maintainer: @escamoteur
https://pub.dev/packages/get_it
MIT License
1.36k stars 149 forks source link

Ability to know registered object's _ServiceFactoryType type #306

Closed Tkko closed 1 year ago

Tkko commented 1 year ago

Hey, I'd like to know whether registered object is singleton or not. Use case: I have a wrapper widget around Cubit/Bloc which retrieves correct instance of bloc from getIt, but I have an issue when I'm trying to close/dispose the bloc, there are two cases

  1. Bloc is registered as factory, meaning it okay to close it after its widget disposes
  2. Bloc is Singleton/lazy and I don't want to close it after its widget disposes, but I can't tell if bloc is singleton or not without passing specific parameter to cubit wrapper widget

Possible solution would be to expose _ServiceFactoryType and call getIt.type() or getIt.typeOf<T>()

escamoteur commented 1 year ago

I know that it's been a long time, but I had to make a longer break due to health issues. Why not simply add a bool flag inside the Bloc object that tells if it can be disposed? or isn't it clear when the Bloc is created if it's used inside a factory or singleton?