StackedLocator class
A thin wrapper around get_it to reduce the number of direct dependencies the user has to depend on.
Constructors
- StackedLocator.asNewInstance()
-
factory
Properties
- allowReassignment ↔ bool
-
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 ofGetIt. 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= truegetter/setter pair - hashCode → int
-
The hash code for this object.
no setterinherited
- locator ↔ GetIt
-
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
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, anWaitingTimeOutExceptionwill 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 extends Object> ({String? instanceName, dynamic param1, dynamic param2}) → T -
Callable class so that you can write
GetIt.instance<MyType>instead ofGetIt.instance.get<MyType> -
get<
T extends Object> ({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 extends Object> ({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 extends Object> ({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, anWaitingTimeOutExceptionwill 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 extends Object> ({Object? instance, String? instanceName}) → bool -
Checks if an async Singleton defined by an
instance, a typeTor aninstanceNameis ready without waiting -
isRegistered<
T extends Object> ({Object? instance, String? instanceName}) → bool -
Tests if an
instanceof an object or aTypeTor a nameinstanceNameis registered inside GetIt -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
popScope(
) → Future< void> - Disposes all factories/Singletons that have ben registered in this scope and pops (destroys) the scope so that the previous scope gets active again. if you provided dispose functions on registration, they will be called. if you passed a dispose function when you pushed this scope it will be calles before the scope is popped. As dispose funcions can be async, you should await this function.
-
popScopesTill(
String name) → Future< bool> -
if you have a lot of scopes with names you can pop (see popScope) all
scopes above the scope with
nameincluding that scope Scopes are poped in order from the top As dispose funcions can be async, you should await this function. it no scope withnameexists, nothing is popped andfalseis returned -
pushNewScope(
{String? scopeName, ScopeDisposeFunc? dispose}) → void -
Creates a new registration scope. If you register types after creating
a new scope they will hide any previous registration of the same type.
Scopes allow you to manage different live times of your Objects.
scopeNameif you name a scope you can pop all scopes above the named one by using the name.disposefunction that will be called when you pop this scope. The scope is still valied while it is executed -
registerEnvironment(
{String? environment, EnvironmentFilter? environmentFilter}) → void -
registerFactory<
T extends Object> (FactoryFunc< T> factoryfunc, {String? instanceName, Set<String> ? registerFor}) → 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 extends Object> (FactoryFuncAsync< T> factoryfunc, {String? instanceName, Set<String> ? registerFor}) → 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 extends Object, P1, P2> (FactoryFuncParam< T, P1?, P2?> factoryfunc, {String? instanceName, Set<String> ? registerFor}) → 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 extends Object, P1, P2> (FactoryFuncParamAsync< T, P1?, P2?> factoryfunc, {String? instanceName, Set<String> ? registerFor}) → 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 extends Object> (FactoryFunc< T> factoryfunc, {String? instanceName, DisposingFunc<T> ? dispose, Set<String> ? registerFor}) → 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 extends Object> (FactoryFuncAsync< T> factoryFunc, {String? instanceName, DisposingFunc<T> ? dispose, Set<String> ? registerFor}) → 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 extends Object> (T instance, {String? instanceName, bool? signalsReady, DisposingFunc< T> ? dispose, Set<String> ? registerFor}) → 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 extends Object> (FactoryFuncAsync< T> factoryfunc, {String? instanceName, Iterable<Type> ? dependsOn, bool? signalsReady, DisposingFunc<T> ? dispose, Set<String> ? registerFor}) → 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 extends Object> (FactoryFunc< T> factoryFunc, {String? instanceName, Iterable<Type> ? dependsOn, bool? signalsReady, DisposingFunc<T> ? dispose, Set<String> ? registerFor}) → 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). -
removeRegistrationIfExists<
T extends Object> ({String? instanceName}) → void -
reset(
{bool dispose = true}) → Future< void> -
Clears all registered types. Handy when writing unit tests
If you provided dispose function when registering they will be called
disposeiffalseit only resets without calling any dispose functions As dispose funcions can be async, you should await this function. -
resetLazySingleton<
T extends Object> ({T? 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. This function overrides the disposing you might have provided when registering. -
resetScope(
{bool dispose = true}) → Future< void> -
Clears all registered types for the current scope
If you provided dispose function when registering they will be called
disposeiffalseit only resets without calling any dispose functions As dispose funcions can be async, you should await this function. -
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 -
toString(
) → String -
A string representation of this object.
inherited
-
unregister<
T extends Object> ({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. This function overrides the disposing you might have provided when registering.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → StackedLocator
-
access to the Singleton instance of GetIt
no setter