UseDependency<T extends Object> constructor

UseDependency<T extends Object>([
  1. String? id
])

A RtHook that allows to manages a dependency of T with/without id.

final useAppController = UseDependency<AppController>();
final useOtherControllerWithId = UseDependency<OtherController>('uniqueId');

Use instance getter to get the T instance:

final useAppController = UseDependency<AppController>();
print(useAppController.instance);

The instance getter returns null, if the T dependency is not found or it hasn't created yet. You can wait for the instance to be created, using UseEffect:

final useAppController = UseDependency<AppController>();
print(useAppController.instance); // return null

UseEffect(() {
  print(useAppController.instance); // return instance of AppController
}, [useAppController]);

The instance must be created by DependencyInjection using the following methods:

  • Rt.get: {@macro get}
  • Rt.create: {@macro create}
  • Rt.builder: {@macro builder}
  • Rt.singleton: {@macro builder}

or created by RtProvider of flutter_reactter

UseDependency providers the following constructors:

IMPORTANT You should call dispose when it's no longer needed.

See also:

  • DependencyInjection, a dependency manager.
  • UseEffect, a side-effect manager.

Implementation

UseDependency([this.id]) {
  _instance = Rt.find<T>(id);
  _listen();
}