How get_it flutter package deals with Garbage Collector as it registers all the instance at start

1.4k Views Asked by At

I have a naive question, I have read about the flutter dependency injection package get_it, it registers all the instances at start like the service locator design pattern, my question is how it deals with a garbage collector and when it frees the objects? Does it store all the instances in memory at start till app lifeCycle?

To be honest, I know very little about Service locator design pattern otherwise I might have understood this package clearly, Now I need your help on how it works under the hood? what if we need to register an instance at run time with some dynamic data coming from API then how can we do that?

Your help will be appreciated thanks in advance

1

There are 1 best solutions below

4
Bram On

Per the documentation, you can unregister a previously registered instance as follows:

/// Unregister an [instance] of an object or a factory/singleton by Type [T] or by name [instanceName]
/// if you need to dispose some resources before the reset, you can
/// provide a [disposingFunction]. This function overrides the disposing
/// you might have provided when registering.
void unregister<T>({Object instance,String instanceName, void Function(T) disposingFunction})

Service objects are often need throughout the life of an application, in which case you'll never need to unregister.