GetIt class

Very simple and easy to use service locator You register your object creation factory or an instance of an object with registerFactory, registerSingleton or registerLazySingleton And retrieve the desired object using get or call your locator das as function as its a callable class Additionally GetIt offers asynchronous creation function as well as functions to synchronize the async initialization of multiple Singletons

Constructors

GetIt.asNewInstance()
If you need more than one instance of GetIt you can use asNewInstance() You should prefer to use the instance() method to access the global instance of GetIt.
factory

Properties

allowReassignment ↔ bool
By default it's not allowed to register a type a second time. If you really need to you can disable the asserts by settingallowReassignment= true
read / write
manualReady → Future
read-only
hashCode → int
The hash code for this object. [...]
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

allReady({Duration timeout}) → Future<void>
returns a Future that completes if all asynchronously created Singletons are ready This can be used inside a FutureBuilder to change the UI as soon as all initialization is done If you pass a timeout, an WaitingTimeOutException will be thrown if not all Singletons were ready in the given time. The Exception contains details on which Singletons are not ready yet.
allReadySync() → bool
Returns if all async Singletons are ready
call<T>([String instanceName]) → T
Callable class so that you can write GetIt.instance<MyType> instead of GetIt.instance.get<MyType>
get<T>([String instanceName]) → T
retrieves or creates an instance of a registered type T depending on the registration function used for this type or based on a name.
getAsync<T>([String instanceName]) → Future<T>
Returns an Future of an instance that is created by an async factory or a Singleton that is not ready with its initialization.
isReady<T>({Object instance, String instanceName, Duration timeout}) → Future<void>
Returns a Future that is completed when a given registered Factories/Singletons has signalled that it is ready T Type of the factory/Singleton to be waited for instance registered instance to be waited for instanceName factory/Singleton to be waited for that was registered by name instead of a type. You should only use one of the If you pass a timeout, an WaitingTimeOutException will be thrown if not all Singletons were ready in the given time. The Exception contains details on which Singletons are not ready yet.
isReadySync<T>({Object instance, String instanceName}) → bool
Checks if an async Singleton defined by an instance, a type T or an instanceName is ready
registerFactory<T>(FactoryFunc<T> func, {String instanceName}) → void
registers a type so that a new instance will be created on each call of get on that type T type to register func 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
registerFactoryAsync<T>(FactoryFuncAsync<T> func, {String instanceName}) → void
We use a separate function for the async registration instead just a new parameter to make the intention explicit T type to register func 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
registerLazySingleton<T>(FactoryFunc<T> func, {String instanceName}) → void
registers a type as Singleton by passing a factory function that will be called on the first call of get on that type T type to register func 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 registerLazySingleton does not influence allReady
registerLazySingletonAsync<T>(SingletonProviderFunc<T> providerFunc, {String instanceName}) → void
registers a type as Singleton by passing an asynchronous factory function which has to return the that will be returned on each call of get on that type. This is a rather esoteric requirement so you should seldom have the need to use it. This factory function providerFunc isn't called immediately but wait till the first call by getAsync() or isReady() is made To control if an async Singleton has completed its providerFunc gets a Completer passed as parameter that has to be completed to signal that this instance is ready. Therefore you have to ensure that the instance is ready before you use get on it or use getAsync() to wait for the completion. You can check if the instance is ready by using isReady() and isReadySync(). providerFunc when it returns an object and not a Future the object has to complete the completer itself. if it returns a future the completer will be completed automatically when that future completes. instanceName if you provide a value here your instance 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. registerLazySingletonAsync does not influence allReady
registerSingleton<T>(T instance, {String instanceName}) → void
registers a type as Singleton by passing an instance of that type that will be returned on each call of get on that type T type to register instanceName if you provide a value here your instance 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
registerSingletonAsync<T>(SingletonProviderFunc<T> providerFunc, {String instanceName, Iterable<Type> dependsOn}) → void
registers a type as Singleton by passing an asynchronous factory function which has to return the that will be returned on each call of get on that type. To control if an async Singleton has completed its initialisation providerFunc gets a Completer passed as parameter that has to be completed to signal that this instance is ready. Therefore you have to ensure that the instance is ready before you use get on it or use getAsync() to wait for the completion. You can check if the instance is ready by using isReady() and isReadySync(). providerFunc is executed immediately. If it returns an object and not a Future the object has to complete the completer itself. if it returns a future the completer will be completed automatically when that future completes instanceName if you provide a value here your instance 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 dependsOn if this instance depends on other registered async instances before it can be initilaized you can either orchestrate this manually using isReady() or pass a list of the type that the instance depends on here. The async factory will wait to be executed till this types are ready.
reset() → void
Clears all registered types. Handy when writing unit tests
resetLazySingleton<T>({Object instance, String instanceName, void disposingFunction(T)}) → void
Clears the instance of a lazy singleton registered type, being able to call the factory function on the next call of get on that type again.
signalReady() → void
You should no longer us this manual mechanism to sync your app startup
unregister<T>({Object instance, String instanceName, void disposingFunction(T)}) → void
Unregister an instance of an object or a factory/singleton by Type T or by name instanceName if you need to dispose any resources you can do it using disposingFunction function that provides a instance of your class to be disposed
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) → bool
The equality operator. [...]
inherited

Static Properties

I GetIt
Short form to access the instance of GetIt
read-only
instance GetIt
access to the Singleton instance of GetIt
read-only