UseDependency
UseDependency is a hook that allows to manage a dependency.
Syntax
-
UseDependency: Default constructor to get theTdependency. It’s similar to usingRt.find.UseDependency<T>({String? id,String? debugLabel}); -
UseDependency.get: Get aTdependency inmediately. It’s similar to usingRt.get.UseDependency<T>.get({String? id,String? debugLabel}); -
UseDependency.register: Register a builder function to create aTdependency. It’s similar to usingRt.register.UseDependency<T>.register(T builder(), {String? id,DependencyMode mode = DependencyMode.builder,String? debugLabel,},); -
UseDependency.create: Registers, creates and gets aTdependency inmediately. It’s similar to usingRt.create.UseDependency<T>.create(T builder(), {String? id,DependencyMode mode = DependencyMode.builder,String? debugLabel,},); -
UseDependency.lazyBuilder: Registers a builder function, to create a instance of theTdependency as Builder mode. It’s similar to usingRt.lazyBuilder.UseDependency<T>.lazyBuilder(T builder(), {String? id,String? debugLabel},); -
UseDependency.lazyFactory: Registers a builder function, to create a instance of theTdependency as Factory mode. It’s similar to usingRt.lazyFactory.UseDependency<T>.lazyFactory(T builder(), {String? id,String? debugLabel},); -
UseDependency.lazySingleton: Registers a builder function, to create a instance of theTdependency as Singleton mode. It’s similar to usingRt.lazySingleton.UseDependency<T>.lazySingleton(T builder(), {String? id,String? debugLabel},); -
UseDependency.builder: Registers, creates and gets aTdependency inmediately as Builder mode. It’s similar to usingRt.builder.UseDependency<T>.builder(T builder(), {String? id,DependencyMode mode = DependencyMode.builder,String? debugLabel,},); -
UseDependency.factory: Registers, creates and gets aTdependency inmediately as Factory mode. It’s similar to usingRt.factory.UseDependency<T>.factory(T builder(), {String? id,DependencyMode mode = DependencyMode.builder,String? debugLabel,},); -
UseDependency.singleton: Registers, creates and gets aTdependency inmediately as Singleton mode. It’s similar to usingRt.singleton.UseDependency<T>.singleton(T builder(), {String? id,DependencyMode mode = DependencyMode.builder,String? debugLabel,},);
Parameters
id: (optional) AStringused to identify the dependency of typeT.builder: A function that returns an instance of the dependency of typeT.mode: (optional) ADependencyModethat defines the dependency mode. Defaults toDependencyMode.builder. Learn more in DependencyMode.debugLabel: (optional) AStringused to identify the hook in the DevTools extension.
Properties and methods
instance: A getter property to get the dependency instance ofTtype.
Properties and methods inherited from RtState
debugLabel: A string that represents the label of the state object for debugging purposes.debugInfo: A map that contains debug information about the state object.
-
update: Executes a callback function and notify its observers that the state has changed. When it is invoked, it emits two lifecycle events to signal the state transition:Lifecycle.willUpdateis emitted first, indicating the impending update.Lifecycle.didUpdateis emitted once the update process is complete.
-
notify: Forces the state to notify its observers. Unlikeupdate, it emits only theLifecycle.didUpdateevent, as it doesn’t involve any preparatory steps before the notification. -
bind: Establishes a connection between the state and a specific instance. This connection allows the instance to reactively update based on changes to the state. By binding the state, the instance becomes aware of changes to the state and can appropriately reflect those changes in its behavior. -
unbind: Releases the connection between the state and the instance. When unbinding, the instance will no longer receive updates from the state. This can be useful when an instance is no longer actively using the state or when it needs to detach from the state temporarily or permanently. -
dispose: Is responsible for cleaning up the state and any associated observers or resources. Disposing of the state ensures that it is properly released and no longer consumes memory or processing resources unnecessarily.
Usage
Finding a dependency
Use the default constructor to obtain the dependency of T type, e.g.:
3 collapsed lines
1import 'package:reactter/reactter.dart';2
3class MyController {4 final uDependency = UseDependency<MyDependency>();5 collapsed lines
5
6 const MyController() {7 print("Dependency instance: ${uDependency.instance}");8 }9}Identifying a dependency
Use the id parameter to identify the dependency. For example, to obtain a dependency by uniqueId :
3 collapsed lines
1import 'package:reactter/reactter.dart';2
3class MyController {4 final uDependency = UseDependency<MyDependency>("uniqueId");5 collapsed lines
5
6 const MyController() {7 print("Dependency instance: ${uDependency.instance}");8 }9}Using the dependency
Use the instance property to access the dependency.
6 collapsed lines
1import 'package:reactter/reactter.dart';2
3class MyController {4 final uDependency = UseDependency<MyDependency>();5
6 const MyController() {7 print("Dependency instance: ${uDependency.instance}");2 collapsed lines
8 }9}Getting a dependency
Ue the UseDependency.get hook to get the dependency instance, e.g.:
3 collapsed lines
1import 'package:reactter/reactter.dart';2
3class MyController {4 final uDependency = UseDependency<MyDependency>.get();5 collapsed lines
5
6 const MyController() {7 print("Dependency instance: ${uDependency.instance}");8 }9}Registering a dependency
Use the UseDependency.register hook to register a builder function to create the dependency instance, e.g.:
3 collapsed lines
1import 'package:reactter/reactter.dart';2
3class MyController {4 final uDependency = UseDependency<MyDependency>.register(() => MyDependency());5 collapsed lines
5
6 const MyController() {7 print("Dependency instance: ${uDependency.instance}");8 }9}Creating a dependency
Use the UseDependency.create hook to create a dependency instance inmediately, e.g.:
3 collapsed lines
1import 'package:reactter/reactter.dart';2
3class MyController {4 final uDependency = UseDependency<MyDependency>.create(() => MyDependency());5 collapsed lines
5
6 const MyController() {7 print("Dependency instance: ${uDependency.instance}");8 }9}Defining the dependency mode
Use the mode parameter to define the dependency mode. For example, to create a singleton dependency:
3 collapsed lines
1import 'package:reactter/reactter.dart';2
3class MyController {4 final uDependency = UseDependency<MyDependency>.create(5 () => MyDependency(),6 mode: DependencyMode.singleton,7 );5 collapsed lines
8
9 const MyController() {10 print("Dependency instance: ${uDependency.instance}");11 }12}