InitializableSingleton class
InitializableSingleton
is a class that extends DependencyRegistration.
It provides a way to register an object as a Singleton to the get_it
locator.
This means the object will be instantiated during the first fetch and then will stay
alive in the memory and the same instance will be returned in the subsequent fetches.
When used with a class implementing the InitializableDependency
interface,
the init
method of the class will be called upon registration, allowing any
necessary asynchronous initialization logic to be executed before the singleton
instance is fetched for the first time.
This is useful when you need to perform some setup or initialization tasks (like setting up a database, making a network request, etc.) before the class can be used.
Example:
class DataCache implements InitializableDependency {
// Implementation details
@override
Future<void> init() async {
// Initialization logic goes here
}
}
Then register it as:
InitializableSingleton(classType: DataCache)
Parameters:
-
classType
: The concrete class to be registered to theget_it
locator. This class should implementInitializableDependency
interface. -
asType
: An abstract class or interface to map theclassType
to. This is useful when you want to abstract the concrete implementation and depend on interfaces. -
environments
: A set of environment names where this registration should be included. Useful for conditionally including a service depending on the running environment. -
instanceName
: An optional instance name that can be used to register multiple objects of the same type. You will need to fetch the object by instance name from theget_it
locator.
- Inheritance
-
- Object
- DependencyRegistration
- InitializableSingleton
Constructors
Properties
- asType → Type?
-
An abstracted class type of service to register
finalinherited
- classType → Type?
-
The type of the service to register
finalinherited
- dispose → Function?
-
finalinherited
-
environments
→ Set<
String> ? -
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- instanceName → String?
-
finalinherited
- param1 → Type?
-
finalinherited
- param2 → Type?
-
finalinherited
- resolveUsing → Function?
-
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited