ServiceProvider class abstract
Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.
To use it, register your services using the registerSingleton
and registerTransient
methods before your app starts.
Those methods accepts a function that receives the ServiceProvider
, so you can inject the services into those constructors.
Example:
ServiceProvider.registerSingleton<SingletonService>((optional, required, platform) => SingletonService(sp.required<TransientService>()));
ServiceProvider.registerTransient<TransientService>((optional, required, platform) => TransientService());
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- 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
Static Properties
- platformInfo → PlatformInfo
-
no setter
Static Methods
-
isRegistered<
TService> ({String? key}) → bool -
Returns
true
if the serviceTService
is registered with the specifiedkey
. -
isRegisteredAsSingleton<
TService> ({String? key}) → bool -
Returns
true
if the serviceTService
is registered with the specifiedkey
as singleton. -
isRegisteredAsTransient<
TService> ({String? key}) → bool -
Returns
true
if the serviceTService
is registered with the specifiedkey
as singleton. -
optional<
TService> ({String? key}) → TService? -
Returns a registered
TService
. IfTService
was not registered, returns null. -
override<
TService> (InjectorDelegate< TService> constructor, {String? key}) → void - Overrides a previous or future registration (for example, to mock concrete implementations in unit tests)
-
registerOrReplaceSingleton<
TService> (InjectorDelegate< TService> constructor, {String? key}) → void - Register a SINGLETON service or replace it if it was already registered
-
registerOrReplaceTransient<
TService> (InjectorDelegate< TService> constructor, {String? key}) → void - Register a transient service or replace it if it was already registered
-
registerSingleton<
TService> (InjectorDelegate< TService> constructor, {String? key}) → void -
Register a constructor as a singleton service (i.e.: the
TService
will always point to the same instance) -
registerSingletonIfNotRegistered<
TService> (InjectorDelegate< TService> constructor, {String? key}) → void -
Register a constructor as a singleton service (i.e.: the
TService
will always point to the same instance), if none is registered yet -
registerTransient<
TService> (InjectorDelegate< TService> constructor, {String? key}) → void -
Register a constructor as a transient service (i.e.: the
TService
will be created each time the ServiceProvider requires it) -
registerTransientIfNotRegistered<
TService> (InjectorDelegate< TService> constructor, {String? key}) → void -
replace<
TService> (InjectorDelegate< TService> constructor, {String? key}) → void - Replaces a previous registration
-
required<
TService> ({String? key}) → TService -
Returns a registered
TService
. IfTService
was not registered, returns aServiceNotRegisteredException
. -
unregister<
TService> ({String? key, bool throwsExceptionIfNotRegistered = false}) → void -
key
must be provided when aTService
is ambiguous and it was registered with a non null key (seeregisterSingleton
andregisterTransient
methods to know more about thekey
argument) -
unregisterAll(
) → void - Unregister all registered services (this is usefull, for instance, in test environments)