UseDependency
UseDependency
is a RtHook that allows to manage a dependency.
Constructors and factories
-
UseDependency
: Default constructor to get theT
dependency. It’s similar to usingRt.find
.UseDependency<T>([String? id]); -
UseDependency.get
: Get aT
dependency inmediately. It’s similar to usingRt.get
.UseDependency<T>.get([String? id]); -
UseDependency.register
: Register a builder function to create aT
dependency. It’s similar to usingRt.register
.UseDependency<T>.register(T Function() builder, {String? id,DependencyMode mode = DependencyMode.builder,}); -
UseDependency.create
: Registers, creates and gets aT
dependency inmediately. It’s similar to usingRt.create
.UseDependency<T>.create(T Function() builder, {String? id,DependencyMode mode = DependencyMode.builder,}); -
UseDependency.lazyBuilder
: Registers a builder function, to create a instance of theT
dependency as Builder mode. It’s similar to usingRt.lazyBuilder
.UseDependency<T>.lazyBuilder(T Function() builder, [String? id]); -
UseDependency.lazyFactory
: Registers a builder function, to create a instance of theT
dependency as Factory mode. It’s similar to usingRt.lazyFactory
.UseDependency<T>.lazyFactory(T Function() builder, [String? id]); -
UseDependency.lazySingleton
: Registers a builder function, to create a instance of theT
dependency as Singleton mode. It’s similar to usingRt.lazySingleton
.UseDependency<T>.lazySingleton(T Function() builder, [String? id]); -
UseDependency.builder
: Registers, creates and gets aT
dependency inmediately as Builder mode. It’s similar to usingRt.builder
.UseDependency<T>.builder(T Function() builder, {String? id,DependencyMode mode = DependencyMode.builder,}); -
UseDependency.factory
: Registers, creates and gets aT
dependency inmediately as Factory mode. It’s similar to usingRt.factory
.UseDependency<T>.factory(T Function() builder, {String? id,DependencyMode mode = DependencyMode.builder,}); -
UseDependency.singleton
: Registers, creates and gets aT
dependency inmediately as Singleton mode. It’s similar to usingRt.singleton
.UseDependency<T>.singleton(T Function() builder, {String? id,DependencyMode mode = DependencyMode.builder,});
Arguments
id
: An optionalString
value to identify the dependency ofT
type.-
builder
: A function that returns an instance of the dependency ofT
type. mode
: An optionalDependencyMode
value to define the dependency mode. Default value isDependencyMode.builder
. Learn more about it in DependencyMode.
Properties and methods
instance
: A getter property to get the dependency instance ofT
type.
-
Methods inherited from
RtState
(Learn more here):-
update
: A method to notify changes after run a set of instructions. -
refresh
: A method to force to notify changes. - *
bind
: A method to bind an instance to it. - *
unbind
: A method to unbind an instance to it. - *
dispose
: A method to remove all listeners and free resources.
-
Usage
Finding a dependency
You can 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
You can use the id
argument 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
You can 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
You can use 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
You can 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
You can 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
You can use the mode
argument 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}