SL class

A minimalist service locator for straightforward dependency injection. No async support, no scopes, no modules, no tags. Just simple registration and retrieval of instances.

Usage:

// Registering a factory (new instance each time)
SL.I.registerFactory<MyService>(() => MyServiceImpl());

// Registering a singleton (same instance every time)
SL.I.registerSingleton<MyRepository>(() => MyRepositoryImpl());

// Retrieving instances
final service = SL.I.get<MyService>();
final repository = SL.I.get<MyRepository>();

Throws a StateError if the requested type is not registered.

Constructors

SL()
factory

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

get<T>() → T
Get an instance of type T. Throws if not registered.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerFactory<T>(FactoryFunc<T> factory) → void
Register a factory that will produce a new instance each time. The factory has access to the injector for resolving dependencies.
registerLazySingleton<T>(FactoryFunc<T> factory, {bool overwrite = false}) → void
Register a lazy singleton (instance created on first use). The factory has access to the injector for resolving dependencies.
registerSingleton<T>(T instance, {bool overwrite = false}) → void
Register a singleton instance (same instance returned every time). Please note that the instance is created at registration time with no access to the injector. This means that dependencies must be provided manually and cannot be injected automatically. To register a singleton that requires dependencies, consider using registerLazySingleton instead.
reset() → void
Clear all registrations (useful in tests).
toString() → String
A string representation of this object.
inherited
unregister<T>() → void
Unregister a type.

Operators

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

Static Properties

I → SL
Shortcut to the singleton instance.
no setter
instance → SL
Singleton instance.
final