Skip to content

UseDependency

UseDependency is a RtHook that allows to manage a dependency.

Constructors and factories

  • UseDependency : Default constructor to get the T dependency. It’s similar to using Rt.find .

    UseDependency<T>([String? id]);
  • UseDependency.get : Get a T dependency inmediately. It’s similar to using Rt.get .

    UseDependency<T>.get([String? id]);
  • UseDependency.register : Register a builder function to create a T dependency. It’s similar to using Rt.register .

    UseDependency<T>.register(T Function() builder, {
    String? id,
    DependencyMode mode = DependencyMode.builder,
    });
  • UseDependency.create : Registers, creates and gets a T dependency inmediately. It’s similar to using Rt.create .

    UseDependency<T>.create(T Function() builder, {
    String? id,
    DependencyMode mode = DependencyMode.builder,
    });
  • UseDependency.lazyBuilder : Registers a builder function, to create a instance of the T dependency as Builder mode. It’s similar to using Rt.lazyBuilder .

    UseDependency<T>.lazyBuilder(T Function() builder, [String? id]);
  • UseDependency.lazyFactory : Registers a builder function, to create a instance of the T dependency as Factory mode. It’s similar to using Rt.lazyFactory .

    UseDependency<T>.lazyFactory(T Function() builder, [String? id]);
  • UseDependency.lazySingleton : Registers a builder function, to create a instance of the T dependency as Singleton mode. It’s similar to using Rt.lazySingleton .

    UseDependency<T>.lazySingleton(T Function() builder, [String? id]);
  • UseDependency.builder : Registers, creates and gets a T dependency inmediately as Builder mode. It’s similar to using Rt.builder .

    UseDependency<T>.builder(T Function() builder, {
    String? id,
    DependencyMode mode = DependencyMode.builder,
    });
  • UseDependency.factory : Registers, creates and gets a T dependency inmediately as Factory mode. It’s similar to using Rt.factory .

    UseDependency<T>.factory(T Function() builder, {
    String? id,
    DependencyMode mode = DependencyMode.builder,
    });
  • UseDependency.singleton : Registers, creates and gets a T dependency inmediately as Singleton mode. It’s similar to using Rt.singleton .

    UseDependency<T>.singleton(T Function() builder, {
    String? id,
    DependencyMode mode = DependencyMode.builder,
    });

Arguments

  • id: An optional String value to identify the dependency of T type.
  • builder : A function that returns an instance of the dependency of T type.
  • mode: An optional DependencyMode value to define the dependency mode. Default value is DependencyMode.builder. Learn more about it in DependencyMode.

Properties and methods

  • instance: A getter property to get the dependency instance of T 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
1
import 'package:reactter/reactter.dart';
2
3
class 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
1
import 'package:reactter/reactter.dart';
2
3
class 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
1
import 'package:reactter/reactter.dart';
2
3
class 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
1
import 'package:reactter/reactter.dart';
2
3
class 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
1
import 'package:reactter/reactter.dart';
2
3
class 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
1
import 'package:reactter/reactter.dart';
2
3
class 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
1
import 'package:reactter/reactter.dart';
2
3
class 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
}