find<T> static method

T find<T>({
  1. String? tag,
})

Retrieves a registered dependency instance.

If the dependency was registered with lazyPut, it will be instantiated on first access. Subsequent calls return the cached instance.

Parameters:

  • tag: Optional identifier if multiple instances exist

Returns the dependency instance.

Throws DependencyNotFoundException if not registered.

Example:

// Basic retrieval
final controller = Dependency.find<MyController>();

// With tag
final admin = Dependency.find<UserService>(tag: 'admin');

// Use in widgets
class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final controller = Dependency.find<CounterController>();
    return Observer(
      listenable: controller.count,
      listener: (value) => Text('$value'),
    );
  }
}

Implementation

static T find<T>({String? tag}) {
  final key = _getKey(T, tag: tag);

  // Check if already instantiated
  if (_dependencyStore[key] != null) {
    return _dependencyStore[key];
  }

  // Check if lazy builder exists
  if (_lazyBuilders[key] != null) {
    final instance = _lazyBuilders[key]!();
    _dependencyStore[key] = instance;
    return instance;
  }

  throw DependencyNotFoundException(T, tag: tag);
}