AsyncLazyDI extension
An extension on the DIPlugin
class to provide asynchronous lazy dependency injection.
This extension introduces two key functions:
-
registerAsync: Registers an asynchronous function that initializes a dependency lazily. The function is executed only when the dependency is first requested.
-
getAsync: Retrieves the registered asynchronous dependency.
Example:
final diPlugin = DIPlugin();
// Registering an async dependency
diPlugin.registerAsync<SomeService>(() async {
await Future.delayed(Duration(seconds: 2));
return SomeService();
});
// Retrieving the registered dependency
final serviceFuture = diPlugin.getAsync<SomeService>();
serviceFuture.then((service) => print('Service initialized: $service'));
- on
Methods
-
getAsync<
T extends Object> ({String? name}) → Future< T> -
Available on DIPlugin, provided by the AsyncLazyDI extension
Retrieves an asynchronously initialized dependency from the dependency injection container. -
registerAsync<
T extends Object> (Future< T> fn(), {String? name}) → void -
Available on DIPlugin, provided by the AsyncLazyDI extension
Registers a lazily initialized asynchronous dependency in the dependency injection container.