ServiceProvider class

ServiceProvider

The service provider, build from a ServiceCollection

The service can be gotten from this ServiceProvider if it is defined in the ServiceCollection

example:

// create a service collection
var collection = ServiceCollection();
// define a service
collection.addSingleton<TestService>((serviceProvider) => TestService());
// build a service provider
var provider = collection.build();
// get service
var service = provider.get<TestService>();

The ServiceProvider may also be one of the scopes of the parent ServiceProvider The scope ServiceProvider will inherit all services of the parent ServiceProvider

example

// ...
var provider = collection.build();
// build a scoped service provider
var scopedProvider = provider.buildScoped();
// get service from scoped service provider
var service = scopedProvider.get<TestService>();

When building the scope ServiceProvider, services defined by the parent can be overridden

example

// ...
collection.addSingleton<TestService>((serviceProvider) => TestService());
var provider = collection.build();
// build a scoped service provider
var scopedProvider = provider.buildScoped(
 builder: (collection) {
    // override the service
    collection.addSingleton<TestService>((serviceProvider) => MyTestService());
  },
);
// get service from scoped service provider
var service = scopedProvider.get<TestService>();
// service is MyTestService

Properties

hashCode int
The hash code for this object.
no setterinherited
parent ServiceProvider?
the parent provider, if not null, this provider is a scope provider
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scope Object?
no setter
serviceDescriptors Map<Type, ServiceDescriptor>
all of the service descriptors
final

Methods

buildScoped({void builder(ServiceCollection)?, Object? scope}) ServiceProvider
build a sub scope
dispose() → void
dispose the ServiceProvider
find<T>() Iterable<T>
find alive services
findByType(Type type) Iterable
find alive services by type, see the find method
get<T>() → T
Get service from ServiceProvider
getByType(Type type) → dynamic
get service by type, can see the get method
getExistScopedSingleton(Type serviceType) Object?
getExistSingleton(Type serviceType) Object?
getExistTransient(Type serviceType) List<Object>?
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited
tryGet<T>() → T?
try get service, if service not found, return null, see the get method
tryGetByType(Type type) → dynamic
try get service by type, if service not found, return null, see the get method
waitLatestServiceInitialize() FutureOr
Wait for the latest service to initialize
waitServicesInitialize() FutureOr
Wait for all current services to be initialized

Operators

operator ==(Object other) bool
The equality operator.
inherited