lazyPut<T> static method
Registers a lazy dependency builder.
The dependency instance is created only when find is called for the first time. Subsequent calls return the same instance.
This is useful for expensive objects that may not be needed immediately.
Parameters:
builder: Function that creates the dependency instancetag: Optional identifier for multiple instancesfenix: Iftrue, rebuilds the dependency after deletion
Example:
// Basic lazy registration
Dependency.lazyPut<DatabaseService>(() => DatabaseService());
// With tag
Dependency.lazyPut<ApiClient>(
() => ApiClient(baseUrl: 'https://api.example.com'),
tag: 'production',
);
// Phoenix mode
Dependency.lazyPut<CacheManager>(
() => CacheManager(),
fenix: true,
);
Implementation
static void lazyPut<T>(
T Function() builder, {
String? tag,
bool fenix = false,
}) {
final key = _getKey(T, tag: tag);
if (_lazyBuilders[key] != null) {
Logger.warn(
'Lazy builder for type $T is being overwritten',
tag: 'Dependency',
);
}
_lazyBuilders[key] = builder;
if (fenix) {
_fenixKeys.add(key);
}
}