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 functions 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
- 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, bool ignorePendingAsyncCreation: false }) → Future< void> -
returns a Future that completes if all asynchronously created Singletons and any Singleton that had
signalsReady==trueare ready. This can be used inside a FutureBuilder to change the UI as soon as all initialization is done If you pass atimeout, 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. if allReady should not wait for the completion of async Signletons setignorePendingAsyncCreation==true -
allReadySync(
[bool ignorePendingAsyncCreation = false ]) → bool -
Returns if all async Singletons are ready without waiting
if allReady should not wait for the completion of async Signletons set
ignorePendingAsyncCreation==true -
call<
T> ({String instanceName, dynamic param1 dynamic param2 }) → T -
Callable class so that you can write
GetIt.instance<MyType>instead ofGetIt.instance.get<MyType> -
get<
T> ({String instanceName, dynamic param1 dynamic param2 }) → T -
retrieves or creates an instance of a registered type
Tdepending on the registration function used for this type or based on a name. for factories you can pass up to 2 parametersparam1,param2they have to match the types given at registration with registerFactoryParam() -
getAsync<
T> ({String instanceName, dynamic param1 dynamic param2 }) → 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.
for async factories you can pass up to 2 parameters
param1,param2they have to match the types given at registration with registerFactoryParamAsync() -
isReady<
T> ({Object instance String instanceName, Duration timeout, Object callee }) → Future< void> -
Returns a Future that completes if the instance of an Singleton, defined by Type
Tor by nameinstanceNameor by passing the an existinginstance, is ready If you pass atimeout, an WaitingTimeOutException will be thrown if the instance is not ready in the given time. The Exception contains details on which Singletons are not ready at that time.calleeoptional parameter which makes debugging easier. Passthisin here. -
isReadySync<
T> ({Object instance, String instanceName }) → bool -
Checks if an async Singleton defined by an
instance, a typeTor aninstanceNameis ready without waiting -
isRegistered<
T> ({Object instance, String instanceName }) → bool -
Tests if an
instanceof an object or aTypeTor a nameinstanceNameis registered inside GetIt -
registerFactory<
T> (FactoryFunc< T> factoryfunc, { String instanceName }) → void -
registers a type so that a new instance will be created on each call of get on that type
Ttype to registerfactoryfuncfactory function for this typeinstanceNameif 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> factoryfunc, { String instanceName }) → void -
registers a type so that a new instance will be created on each call of getAsync on that type
the creation function is executed asynchronously and has to be accessed with getAsync
Ttype to registerfactoryfuncasync factory function for this typeinstanceNameif 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 -
registerFactoryParam<
T, P1, P2> (FactoryFuncParam< T, P1, P2> factoryfunc, { String instanceName }) → void -
registers a type so that a new instance will be created on each call of get on that type based on
up to two parameters provided to get()
Ttype to registerP1type of param1P2type of param2 if you use only one parameter pass void herefactoryfuncfactory function for this type that accepts two parametersinstanceNameif 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 [...] -
registerFactoryParamAsync<
T, P1, P2> (FactoryFuncParamAsync< T, P1, P2> factoryfunc, { String instanceName }) → void -
registers a type so that a new instance will be created on each call of getAsync
on that type based on up to two parameters provided to getAsync()
the creation function is executed asynchronously and has to be accessed with getAsync
Ttype to registerP1type of param1P2type of param2 if you use only one parameter pass void herefactoryfuncfactory function for this type that accepts two parametersinstanceNameif 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> factoryfunc, { 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
Ttype to registerfactoryfuncfactory function for this typeinstanceNameif 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 however you can wait for and be dependent on a LazySingleton. -
registerLazySingletonAsync<
T> (FactoryFuncAsync< T> factoryFunc, { String instanceName }) → void -
registers a type as Singleton by passing a async factory function that will be called
on the first call of
getAsncon that type This is a rather esoteric requirement so you should seldom have the need to use it. This factory functionfactoryFuncisn't called immediately but wait till the first call by getAsync() or isReady() is made To control if an async Singleton has completed itsfactoryFuncgets aCompleterpassed 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 wait/check if the instance is ready by using isReady() and isReadySync().instanceNameif 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 however you can wait for and be dependent on a LazySingleton. -
registerSingleton<
T> (T instance, { String instanceName, bool signalsReady }) → void -
registers a type as Singleton by passing an
instanceof that type that will be returned on each call of get on that typeTtype to registerinstanceNameif 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 IfsignalsReadyis set totrueit means that the future you can get fromallReady()cannot complete until this this instance was signalled ready by callingsignalsReady(instance). -
registerSingletonAsync<
T> (FactoryFuncAsync< T> factoryfunc, { String instanceName, Iterable<Type> dependsOn, bool signalsReady }) → void -
registers a type as Singleton by passing an asynchronous factory function which has to return the instance
that will be returned on each call of get on that type.
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 wait/check if the instance is ready by using isReady() and isReadySync().
factoryfuncis executed immediately if there are no dependencies to other Singletons (see below). As soon as it returns, this instance is marked as ready unless you don't setsignalsReady==trueinstanceNameif 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 recommendeddependsOnif this instance depends on other registered Singletons before it can be initilaized you can either orchestrate this manually using isReady() or pass a list of the types that the instance depends on here.factoryFuncwon't get executed till this types are ready. IfsignalsReadyis set totrueit means that the future you can get fromallReady()cannot complete until this this instance was signalled ready by callingsignalsReady(instance). In that case no automatic ready signal is made after completion offactoryfunc -
registerSingletonWithDependencies<
T> (FactoryFunc< T> factoryFunc, { String instanceName, Iterable<Type> dependsOn, bool signalsReady }) → void -
registers a type as Singleton by passing an factory function of that type
that will be called on each call of get on that type
Ttype to registerinstanceNameif 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 recommendeddependsOnif this instance depends on other registered Singletons before it can be initilaized you can either orchestrate this manually using isReady() or pass a list of the types that the instance depends on here.factoryFuncwon't get executed till this types are ready.funcis called IfsignalsReadyis set totrueit means that the future you can get fromallReady()cannot complete until this this instance was signalled ready by callingsignalsReady(instance). -
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,
being able to call the factory function on the next call
of get on that type again.
you select the lazy Singleton you want to reset by either providing
an
instance, its registered typeTor its registration name. if you need to dispose some resources before the reset, you can provide adisposingFunction -
signalReady(
Object instance) → void -
Used to manually signal the ready state of a Singleton.
If you want to use this mechanism you have to pass
signalsReady==truewhen registering the Singleton. Ifinstancehas a value GetIt will search for the responsible Singleton and completes all futures that might be waited for by isReady If all waiting singletons have signalled ready the future you can get from allReady is automatically completed [...] -
unregister<
T> ({Object instance, String instanceName, void disposingFunction(T) }) → void -
Unregister an
instanceof an object or a factory/singleton by TypeTor by nameinstanceNameif you need to dispose any resources you can do it usingdisposingFunctionfunction 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