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 , {
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 , {
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 , {
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 , {
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 , {
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 .
Note
Some constructors and factories don’t have all the arguments. For example,
builder
is not available in UseDependency
and UseDependency . get
, and mode
is available only in UseDependency . register
and UseDependency . create
.
Properties and methods
instance
: A getter property to get the dependency instance of T
type.
Usage
Finding a dependency
You can use the default constructor to obtain the dependency of T
type, e.g.:
import 'package:reactter/reactter.dart' ;
final uDependency = UseDependency < MyDependency > () ;
print ( "Dependency instance: ${ uDependency . instance } " ) ;
Note
The default constructor only will find the dependency by T
type. Note that the instance
property will be null
if the dependency is not created already.
To register and create the dependency instance, using the UseDependency . create
hook instead. Learn more about it in Creating a dependency .
If only you need to retrieve the dependency instance, using the UseDependency . get
hook instead. Learn more about it in Getting a dependency .
Identifying a dependency
You can use the id
argument to identify the dependency. For example, to obtain a dependency by uniqueId
:
import 'package:reactter/reactter.dart' ;
final uDependency = UseDependency < MyDependency > ( " uniqueId " ) ;
print ( "Dependency instance: ${ uDependency . instance } " ) ;
Using the dependency
You can use the instance
property to access the dependency.
import 'package:reactter/reactter.dart' ;
final uDependency = UseDependency < MyDependency > () ;
print ( "Dependency instance: ${ uDependency . instance } " ) ;
Tip
If the dependency is not registered and created, the instance
property will be null
.
To avoid this, you can use the UseEffect
hook to get the dependency when it is created, e.g.:
import 'package:reactter/reactter.dart' ;
final uDependency = UseDependency < MyDependency > () ;
if (uDependency . instance != null ) {
print ( "Dependency instance: ${ uDependency . instance } " ) ;
Getting a dependency
You can use the UseDependency . get
hook to get the dependency instance, e.g.:
import 'package:reactter/reactter.dart' ;
final uDependency = UseDependency < MyDependency > . get () ;
print ( "Dependency instance: ${ uDependency . instance } " ) ;
Note
UseDependency . get
hook will get the dependency instance inmediately. If the dependency is not created yet, it will create it, but if the dependency is not registered, the instance
property will be null
.
To ensure that the dependency is created, you can using the UseDependency . create
hook instead. Learn more about it in Creating a dependency .
Registering a dependency
You can use the UseDependency . register
hook to register a builder function to create the dependency instance, e.g.:
import 'package:reactter/reactter.dart' ;
final uDependency = UseDependency < MyDependency > . register (() => MyDependency ()) ;
print ( "Dependency instance: ${ uDependency . instance } " ) ;
Note
In above example, it will print Dependency instance: null , because UseDependency . register
hook does not create the dependency instance immediately.
For creating the dependency instance, using the Rt . get
method, e.g.:
import 'package:reactter/reactter.dart' ;
final uDependency = UseDependency < MyDependency > . register (() => MyDependency ()) ;
print ( "Dependency instance: ${ uDependency . instance } " ) ;
Also, you can create the dependency instance inmediately using the UseDependency . create
hook instead. Learn more about it in Creating a dependency .
Creating a dependency
You can use the UseDependency . create
hook to create a dependency instance inmediately, e.g.:
import 'package:reactter/reactter.dart' ;
final uDependency = UseDependency < MyDependency > . create (() => MyDependency ()) ;
print ( "Dependency instance: ${ uDependency . instance } " ) ;
Defining the dependency mode
You can use the mode
argument to define the dependency mode. For example, to create a singleton dependency:
import 'package:reactter/reactter.dart' ;
final uDependency = UseDependency < MyDependency > . create (
mode : DependencyMode . singleton ,
print ( "Dependency instance: ${ uDependency . instance } " ) ;
Note
The mode
argument is available only in UseDependency . register
and UseDependency . create
hooks.
If you need to define the dependency mode directly, use the UseDependency . lazyBuilder
, UseDependency . lazyFactory
, UseDependency . lazySingleton
hooks for registering a dependency in its respective mode.
Likewise, use the UseDependency . builder
, UseDependency . factory
and UseDependency . singleton
hooks instead, for creating a dependency in its respective mode. Learn more about it in Dependency Modes , e.g.:
import 'package:reactter/reactter.dart' ;
class DependenciesRegisters {
DependenciesRegisters () {
UseDependency < MyDependency > . lazyFactory (() => MyDependency ()) ;
UseDependency < MyOtherDependency > . lazySingleton (() => MyOtherDependency ()) ;
UseDependency < MyController > . builder (() => MyController ()) ;
import 'package:flutter_reactter/flutter_reactter.dart' ;
class MyApp extends StatelessWidget {
Widget build ( BuildContext context) {
() => DependenciesRegisters () ,
build : (_ , __ , ___) => MyHomePage () ,
import 'package:reactter/reactter.dart' ;
/// Get MyDependency as Factory mode.
final uDependency = UseDependency < MyDependency > . get () ;
/// Get MyOtherDependency as Singleton mode.
final uOtherDependency = UseDependency < MyOtherDependency > . get () ;
print ( "Dependency instance using Factory mode: ${ uDependency . instance } " ) ;
print ( "Dependency instance using Singleton mode: ${ uOtherDependency . instance } " ) ;