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

ServiceProvider()

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 service TService is registered with the specified key.
isRegisteredAsSingleton<TService>({String? key}) bool
Returns true if the service TService is registered with the specified key as singleton.
isRegisteredAsTransient<TService>({String? key}) bool
Returns true if the service TService is registered with the specified key as singleton.
optional<TService>({String? key}) → TService?
Returns a registered TService. If TService 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. If TService was not registered, returns a ServiceNotRegisteredException.
unregister<TService>({String? key, bool throwsExceptionIfNotRegistered = false}) → void
key must be provided when a TService is ambiguous and it was registered with a non null key (see registerSingleton and registerTransient methods to know more about the key argument)
unregisterAll() → void
Unregister all registered services (this is usefull, for instance, in test environments)