Skip to content

UseDependency

UseDependency is a hook that allows to manage a dependency.

Syntax

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

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

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

    UseDependency<T>.register(
    T builder(), {
    String? id,
    DependencyMode mode = DependencyMode.builder,
    String? debugLabel,
    },
    );
  • UseDependency.create : Registers, creates and gets a T dependency inmediately. It’s similar to using Rt.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 the T dependency as Builder mode. It’s similar to using Rt.lazyBuilder .

    UseDependency<T>.lazyBuilder(
    T builder(), {
    String? id,
    String? debugLabel
    },
    );
  • 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 builder(), {
    String? id,
    String? debugLabel
    },
    );
  • 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 builder(), {
    String? id,
    String? debugLabel
    },
    );
  • UseDependency.builder : Registers, creates and gets a T dependency inmediately as Builder mode. It’s similar to using Rt.builder .

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

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

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

Parameters

  • id: (optional) A String used to identify the dependency of type T .
  • builder: A function that returns an instance of the dependency of type T .
  • mode: (optional) A DependencyMode that defines the dependency mode. Defaults to DependencyMode.builder. Learn more in DependencyMode.
  • debugLabel: (optional) A String used to identify the hook in the DevTools extension.

Properties and methods

  • instance: A getter property to get the dependency instance of T type.
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.willUpdate is emitted first, indicating the impending update.
    • Lifecycle.didUpdate is emitted once the update process is complete.
  • notify : Forces the state to notify its observers. Unlike update , it emits only the Lifecycle.didUpdate event, 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
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

Use the id parameter 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

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

Ue 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

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

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

Use the mode parameter 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
}