ServiceContainer class abstract

A lightweight, hierarchical Dependency Injection (DI) container for Dart/Flutter.

Supports:

Example

final container = Container.root();

container.bind<Logger>((c, _) => ConsoleLogger());
container.singleton<Database>(
  factory: (c, _) => Database.connect(),
  eager: true,
);

await container.initialize();

final logger = container.make<Logger>();
Implementers

Constructors

ServiceContainer()

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

allReady() Future<void>
Waits for all async initialization tasks to complete.
bind<T>({String? name, required FactoryFunction<T> factory}) → void
Registers a transient service.
bindInstance<T>(T instance, {String? name}) → void
Binds an existing instance to the container.
contains<T>({String? name}) bool
Checks if a service is registered in this container or any parent scope.
dispose() Future<void>
Disposes all singletons and runs disposal callbacks in reverse order.
disposeBinding<T>({String? name}) Future<bool>
Disposes a binding
initialize() Future<void>
Initializes all eager singletons.
listRegistrations() Map<String, List<String>>
Returns a map of all registrations for debugging and introspection.
make<T>({String? name, Map<String, dynamic>? options}) → T
Resolves a service instance.
newScope() ServiceContainer
Creates a child scope that inherits parent bindings but isolates singletons.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onDispose(Future<void> callback()) → void
Registers an async cleanup callback to run during dispose.
singleton<T>({String? name, required FactoryFunction<T> factory, bool eager = false}) → void
Registers a singleton service.
toString() String
A string representation of this object.
inherited
tryMake<T>({String? name, Map<String, dynamic>? options}) → T?
Safely resolves a service. Returns null if not registered.

Operators

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