lazyPut<T> static method

void lazyPut<T>(
  1. T builder(), {
  2. String? tag,
  3. bool fenix = false,
})

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 instance
  • tag: Optional identifier for multiple instances
  • fenix: If true, 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);
  }
}