Skip to content

Dependency Injection Methods

Reactter provides several methods to manage dependencies in a more efficient way. These methods are used to create and manipulate dependencies in a more organized way.

Rt.register

The Rt.register method registers a new dependency. It returns true if the dependency is registered successfully, otherwise return false .

Syntax

bool Rt.register<T>(
T Function() builder, {
String? id,
DependencyMode mode = DependencyMode.builder,
});

Parameters

  • builder : A function that returns an instance of the T dependency.
  • id: An optional identifier for the T dependency. If not provided, the dependency is registered by its type( T ).
  • mode: The mode of the dependency. It can be DependencyMode.builder, DependencyMode.factory, or DependencyMode.singleton. The default value is DependencyMode.builder. Learn more about it in DependencyMode.

Example

See the example below to understand how to use the Rt.register method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Register a dependency
7
Rt.register<Counter>(() => Counter());
28 collapsed lines
8
9
// Get the dependency
10
final counter = Rt.get<Counter>();
11
print("counter: ${counter?.hashCode}");
12
13
// Listen to the state changes
14
Rt.on(
15
counter!.count,
16
Lifecycle.didUpdate,
17
(_, __) {
18
print("Count: ${counter.count.value}");
19
},
20
);
21
22
// Update the state
23
counter.increment();
24
counter.decrement();
25
26
// Delete the dependency
27
Rt.delete<Counter>();
28
29
// Error: "Can't update when it's been disposed", because the dependency instance was deleted
30
// counter.increment();
31
32
// Can't get the dependency instance, because the dependency register was deleted
33
final counter2 = Rt.get<Counter>();
34
print("counter2: ${counter2?.hashCode}"); // counter2: null
35
36
// Register a dependency using `id` parameter
37
Rt.register(() => Counter(), id: 'CounterById');
8 collapsed lines
38
39
// Get the dependency using `id` parameter
40
final counterById = Rt.get<Counter>('CounterById');
41
print("counterById: ${counterById?.hashCode}");
42
43
// Delete the dependency using `id` parameter
44
Rt.delete<Counter>('CounterById');
45
46
// Register a dependency using `mode` parameter as singleton mode
47
Rt.register(() => Counter(), mode: DependencyMode.singleton);
27 collapsed lines
48
49
// Get the dependency
50
final counterBySingletonMode = Rt.get<Counter>();
51
print("counterBySingletonMode: ${counterBySingletonMode?.hashCode}");
52
53
// Delete the dependency
54
Rt.delete<Counter>();
55
56
// Can get the dependency again, because it's singleton mode
57
final counterBySingletonMode2 = Rt.get<Counter>();
58
print(
59
"counterBySingletonMode2: ${counterBySingletonMode2?.hashCode}",
60
);
61
62
// Use `onlyInstance` parameter to delete the dependency instance only
63
Rt.destroy<Counter>(onlyInstance: true);
64
65
// Can't get the dependency instance, because the dependency register was destroyed
66
final counterBySingletonMode3 = Rt.find<Counter>();
67
print(
68
"counterBySingletonMode3: ${counterBySingletonMode3?.hashCode}",
69
); // counterBySingletonMode3: null
70
71
// If you need to delete the dependency fully, use `Rt.destroy` method by forcing it, because it's singleton mode
72
Rt.destroy<Counter>();
73
74
runApp(MyApp());
75
}
13 collapsed lines
76
77
class MyApp extends StatelessWidget {
78
@override
79
Widget build(BuildContext context) {
80
return MaterialApp(
81
home: Material(
82
child: Center(
83
child: Text('See the output in the terminal'),
84
),
85
),
86
);
87
}
88
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.lazyBuilder

The Rt.lazyBuilder method registers a new dependency as DependencyMode.builder. It returns true if the dependency is registered successfully, otherwise return false .

This method is similar to the Rt.register method but with the DependencyMode.builder mode only.

Syntax

bool Rt.lazyBuilder<T>(
T Function() builder, {
String? id,
});

Parameters

  • builder : A function that returns an instance of the T dependency.
  • id: An optional identifier for the T dependency. If not provided, the dependency is registered by its type( T ).

Example

See the example below to understand how to use the Rt.lazyBuilder method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Register a dependency as builder mode.
7
Rt.lazyBuilder<Counter>(() => Counter());
28 collapsed lines
8
9
// Get the dependency
10
final counter = Rt.get<Counter>();
11
print("counter: ${counter?.hashCode}");
12
13
// Listen to the state changes
14
Rt.on(
15
counter!.count,
16
Lifecycle.didUpdate,
17
(_, __) {
18
print("Count: ${counter.count.value}");
19
},
20
);
21
22
// Update the state
23
counter.increment();
24
counter.decrement();
25
26
// Delete the dependency
27
Rt.delete<Counter>();
28
29
// Error: "Can't update when it's been disposed", because the dependency instance was deleted
30
// counter.increment();
31
32
// Can't get the dependency instance, because the dependency register was deleted
33
final counter2 = Rt.get<Counter>();
34
print("counter2: ${counter2?.hashCode}"); // counter2: null
35
36
// Register a dependency using `id` parameter as builder mode
37
Rt.lazyBuilder(() => Counter(), id: 'CounterById');
9 collapsed lines
38
39
// Get the dependency using `id` parameter
40
final counterById = Rt.get<Counter>('CounterById');
41
print("counterById: ${counterById?.hashCode}");
42
43
// Delete the dependency using `id` parameter
44
Rt.delete<Counter>('CounterById');
45
46
runApp(MyApp());
47
}
13 collapsed lines
48
49
class MyApp extends StatelessWidget {
50
@override
51
Widget build(BuildContext context) {
52
return MaterialApp(
53
home: Material(
54
child: Center(
55
child: Text('See the output in the terminal'),
56
),
57
),
58
);
59
}
60
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.lazyFactory

The Rt.lazyFactory method registers a new dependency as DependencyMode.factory. It returns true if the dependency is registered successfully, otherwise return false .

This method is similar to the Rt.register method but with the DependencyMode.factory mode only.

Syntax

bool Rt.lazyFactory<T>(
T Function() builder, {
String? id,
});

Parameters

  • builder : A function that returns an instance of the T dependency.
  • id: An optional identifier for the T dependency. If not provided, the dependency is registered by its type( T ).

Example

See the example below to understand how to use the Rt.lazyFactory method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Register a dependency as factory mode.
7
Rt.lazyFactory<Counter>(() => Counter());
35 collapsed lines
8
9
// Get the dependency
10
final counter = Rt.get<Counter>();
11
print("counter: ${counter?.hashCode}");
12
13
// Listen to the state changes
14
Rt.on(
15
counter!.count,
16
Lifecycle.didUpdate,
17
(_, __) {
18
print("Count: ${counter.count.value}");
19
},
20
);
21
22
// Update the state
23
counter.increment();
24
counter.decrement();
25
26
// Delete the dependency
27
Rt.delete<Counter>();
28
29
// Error: "Can't update when it's been disposed", because the dependency instance was deleted
30
// counter.increment();
31
32
// Can get the dependency instance, but it's created again, because it's factory mode
33
final counter2 = Rt.get<Counter>();
34
print("counter2: ${counter2?.hashCode}");
35
36
// Delete the dependency instance and its register
37
Rt.destroy<Counter>();
38
39
// Can't get the dependency instance, because the dependency register was deleted using `destroy` method
40
final counter3 = Rt.get<Counter>();
41
print("counter3: ${counter3?.hashCode}"); // counter3: null
42
43
// Register a dependency using `id` parameter as factory mode
44
Rt.lazyFactory(() => Counter(), id: 'CounterById');
9 collapsed lines
45
46
// Get the dependency using `id` parameter
47
final counterById = Rt.get<Counter>('CounterById');
48
print("counterById: ${counterById?.hashCode}");
49
50
// Delete the dependency instance and its register using `id` parameter
51
Rt.destroy<Counter>(id: 'CounterById');
52
53
runApp(MyApp());
54
}
13 collapsed lines
55
56
class MyApp extends StatelessWidget {
57
@override
58
Widget build(BuildContext context) {
59
return MaterialApp(
60
home: Material(
61
child: Center(
62
child: Text('See the output in the terminal'),
63
),
64
),
65
);
66
}
67
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.lazySingleton

The Rt.lazySingleton method registers a new dependency as DependencyMode.singleton. It returns true if the dependency is registered successfully, otherwise return false .

This method is similar to the Rt.register method but with the DependencyMode.singleton mode.

Syntax

bool Rt.lazySingleton<T>(
T Function() builder, {
String? id,
});

Parameters

  • builder : A function that returns an instance of the T dependency.
  • id: An optional identifier for the T dependency. If not provided, the dependency is registered by its type( T ).

Example

See the example below to understand how to use the Rt.lazySingleton method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Register a dependency as singleton mode.
7
Rt.lazySingleton<Counter>(() => Counter());
42 collapsed lines
8
9
// Get the dependency
10
final counter = Rt.get<Counter>();
11
print("counter: ${counter?.hashCode}");
12
13
// Listen to the state changes
14
Rt.on(
15
counter!.count,
16
Lifecycle.didUpdate,
17
(_, __) {
18
print("Count: ${counter.count.value}");
19
},
20
);
21
22
// Update the state
23
counter.increment();
24
counter.decrement();
25
26
// Can't delete the dependency, because it's singleton mode
27
Rt.delete<Counter>();
28
29
// Can update the state, because it's singleton mode
30
counter.increment();
31
32
// Can get the same dependency instanse, because it's singleton mode
33
final counter2 = Rt.get<Counter>();
34
print("counter2: ${counter2?.hashCode}");
35
36
// Delete the dependency instance
37
Rt.destroy<Counter>(onlyInstance: true);
38
39
// Can get the dependency instance, but it's created again, because it's singleton mode
40
final counter3 = Rt.get<Counter>();
41
print("counter3: ${counter3?.hashCode}");
42
43
// Delete the dependency instance and its register
44
Rt.destroy<Counter>();
45
46
// Can't get the dependency instance, because the dependency register was deleted using `destroy` method
47
final counter4 = Rt.get<Counter>();
48
print("counter4: ${counter4?.hashCode}"); // counter4: null
49
50
// Register a dependency using `id` parameter as singleton mode
51
Rt.lazySingleton(() => Counter(), id: 'CounterById');
9 collapsed lines
52
53
// Get the dependency using `id` parameter
54
final counterById = Rt.get<Counter>('CounterById');
55
print("counterById: ${counterById?.hashCode}");
56
57
// Delete the dependency instance and its register using `id` parameter
58
Rt.destroy<Counter>(id: 'CounterById');
59
60
runApp(MyApp());
61
}
13 collapsed lines
62
63
class MyApp extends StatelessWidget {
64
@override
65
Widget build(BuildContext context) {
66
return MaterialApp(
67
home: Material(
68
child: Center(
69
child: Text('See the output in the terminal'),
70
),
71
),
72
);
73
}
74
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.create

The Rt.create method registers a dependency(if not already registered), create its instance(if not created yet) and return it.

Syntax

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

Parameters

  • builder : A function that returns an instance of the T dependency.
  • id: An optional identifier for the T dependency. If not provided, the default instance of the T dependency is used.

Example

See the example below to understand how to use the Rt.create method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Create a dependency
7
final counter = Rt.create<Counter>(() => Counter());
25 collapsed lines
8
print("counter: ${counter?.hashCode}");
9
10
// Listen to the state changes
11
Rt.on(
12
counter!.count,
13
Lifecycle.didUpdate,
14
(_, __) {
15
print("Count: ${counter.count.value}");
16
},
17
);
18
19
// Update the state
20
counter.increment();
21
counter.decrement();
22
23
// Delete the dependency
24
Rt.delete<Counter>();
25
26
// Error: "Can't update when it's been disposed", because the dependency instance was deleted
27
// counter.increment();
28
29
// Can't get the dependency instance, because the dependency register was deleted
30
final counter2 = Rt.get<Counter>();
31
print("counter2: ${counter2?.hashCode}"); // counter2: null
32
33
// Create a dependency using `id` parameter
34
final counterById = Rt.create(() => Counter(), id: 'CounterById');
5 collapsed lines
35
print("counterById: ${counterById?.hashCode}");
36
37
// Delete the dependency using `id` parameter
38
Rt.delete<Counter>('CounterById');
39
40
// Create a dependency using `mode` parameter as singleton mode
41
final counterBySingletonMode = Rt.create(
42
() => Counter(),
43
mode: DependencyMode.singleton,
44
);
24 collapsed lines
45
print("counterBySingletonMode: ${counterBySingletonMode?.hashCode}");
46
47
// Delete the dependency
48
Rt.delete<Counter>();
49
50
// Can get the dependency again, because it's singleton mode
51
final counterBySingletonMode2 = Rt.get<Counter>();
52
print(
53
"counterBySingletonMode2: ${counterBySingletonMode2?.hashCode}",
54
);
55
56
// Use `onlyInstance` parameter to delete the dependency instance only
57
Rt.destroy<Counter>(onlyInstance: true);
58
59
// Can't get the dependency instance, because the dependency register was destroyed
60
final counterBySingletonMode3 = Rt.find<Counter>();
61
print(
62
"counterBySingletonMode3: ${counterBySingletonMode3?.hashCode}",
63
); // counterBySingletonMode3: null
64
65
// If you need to delete the dependency fully, use `Rt.destroy` method by forcing it, because it's singleton mode
66
Rt.destroy<Counter>();
67
68
runApp(MyApp());
69
}
13 collapsed lines
70
71
class MyApp extends StatelessWidget {
72
@override
73
Widget build(BuildContext context) {
74
return MaterialApp(
75
home: Material(
76
child: Center(
77
child: Text('See the output in the terminal'),
78
),
79
),
80
);
81
}
82
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.builder

The Rt.create method registers a dependency as DependencyMode.builder(if not already registered), create its instance(if not created yet) and return it.

This method is similar to the Rt.create method.

Syntax

T Rt.builder<T>(T Function() builder, {
String? id,
});

Parameters

  • builder : A function that returns an instance of the T dependency.
  • id: An optional identifier for the T dependency. If not provided, the default instance of the T dependency is used.

Example

See the example below to understand how to use the Rt.builder method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Create a dependency as builder mode.
7
final counter = Rt.builder<Counter>(() => Counter());
25 collapsed lines
8
print("counter: ${counter?.hashCode}");
9
10
// Listen to the state changes
11
Rt.on(
12
counter!.count,
13
Lifecycle.didUpdate,
14
(_, __) {
15
print("Count: ${counter.count.value}");
16
},
17
);
18
19
// Update the state
20
counter.increment();
21
counter.decrement();
22
23
// Delete the dependency
24
Rt.delete<Counter>();
25
26
// Error: "Can't update when it's been disposed", because the dependency instance was deleted
27
// counter.increment();
28
29
// Can't get the dependency instance, because the dependency register was deleted
30
final counter2 = Rt.get<Counter>();
31
print("counter2: ${counter2?.hashCode}"); // counter2: null
32
33
// Create a dependency using `id` parameter as builder mode
34
final counterById = Rt.builder<Counter>(
35
() => Counter(),
36
id: 'CounterById',
37
);
6 collapsed lines
38
print("counterById: ${counterById?.hashCode}");
39
40
// Delete the dependency using `id` parameter
41
Rt.delete<Counter>('CounterById');
42
43
runApp(MyApp());
14 collapsed lines
44
}
45
46
class MyApp extends StatelessWidget {
47
@override
48
Widget build(BuildContext context) {
49
return MaterialApp(
50
home: Material(
51
child: Center(
52
child: Text('See the output in the terminal'),
53
),
54
),
55
);
56
}
57
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.factory

The Rt.factory method registers a dependency as DependencyMode.factory(if not already registered), create its instance(if not created yet) and return it.

This method is similar to the Rt.create method but with the DependencyMode.factory mode.

Syntax

T Rt.factory<T>(T Function() builder, {
String? id,
});

Parameters

  • builder : A function that returns an instance of the T dependency.
  • id: An optional identifier for the T dependency. If not provided, the default instance of the T dependency is used.

Example

See the example below to understand how to use the Rt.factory method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Create a dependency as factory mode.
7
final counter = Rt.factory<Counter>(() => Counter());
32 collapsed lines
8
print("counter: ${counter?.hashCode}");
9
10
// Listen to the state changes
11
Rt.on(
12
counter!.count,
13
Lifecycle.didUpdate,
14
(_, __) {
15
print("Count: ${counter.count.value}");
16
},
17
);
18
19
// Update the state
20
counter.increment();
21
counter.decrement();
22
23
// Delete the dependency
24
Rt.delete<Counter>();
25
26
// Error: "Can't update when it's been disposed", because the dependency instance was deleted
27
// counter.increment();
28
29
// Can get the dependency instance, but it's created again, because it's factory mode
30
final counter2 = Rt.get<Counter>();
31
print("counter2: ${counter2?.hashCode}");
32
33
// Delete the dependency instance and its register
34
Rt.destroy<Counter>();
35
36
// Can't get the dependency instance, because the dependency register was deleted using `destroy` method
37
final counter3 = Rt.get<Counter>();
38
print("counter3: ${counter3?.hashCode}"); // counter3: null
39
40
// Create a dependency using `id` parameter as factory mode
41
final counterById = Rt.factory(() => Counter(), id: 'CounterById');
6 collapsed lines
42
print("counterById: ${counterById?.hashCode}");
43
44
// Delete the dependency instance and its register using `id` parameter
45
Rt.destroy<Counter>(id: 'CounterById');
46
47
runApp(MyApp());
48
}
13 collapsed lines
49
50
class MyApp extends StatelessWidget {
51
@override
52
Widget build(BuildContext context) {
53
return MaterialApp(
54
home: Material(
55
child: Center(
56
child: Text('See the output in the terminal'),
57
),
58
),
59
);
60
}
61
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.singleton

The Rt.singleton method registers a dependency as DependencyMode.singleton(if not already registered), create its instance(if not created yet) and return it.

This method is similar to the Rt.create method but with the DependencyMode.singleton mode.

Syntax

T Rt.singleton<T>(T Function() builder, {
String? id,
});

Parameters

  • builder : A function that returns an instance of the T dependency.
  • id: An optional identifier for the T dependency. If not provided, the default instance of the T dependency is used.

Example

See the example below to understand how to use the Rt.singleton method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Create a dependency as singleton mode.
7
final counter = Rt.singleton<Counter>(() => Counter());
39 collapsed lines
8
print("counter: ${counter?.hashCode}");
9
10
// Listen to the state changes
11
Rt.on(
12
counter!.count,
13
Lifecycle.didUpdate,
14
(_, __) {
15
print("Count: ${counter.count.value}");
16
},
17
);
18
19
// Update the state
20
counter.increment();
21
counter.decrement();
22
23
// Can't delete the dependency, because it's singleton mode
24
Rt.delete<Counter>();
25
26
// Can update the state, because it's singleton mode
27
counter.increment();
28
29
// Can get the same dependency instanse, because it's singleton mode
30
final counter2 = Rt.get<Counter>();
31
print("counter2: ${counter2?.hashCode}");
32
33
// Delete the dependency instance
34
Rt.destroy<Counter>(onlyInstance: true);
35
36
// Can get the dependency instance, but it's created again, because it's singleton mode
37
final counter3 = Rt.get<Counter>();
38
print("counter3: ${counter3?.hashCode}");
39
40
// Delete the dependency instance and its register
41
Rt.destroy<Counter>();
42
43
// Can't get the dependency instance, because the dependency register was deleted using `destroy` method
44
final counter4 = Rt.get<Counter>();
45
print("counter4: ${counter4?.hashCode}"); // counter4: null
46
47
// Create a dependency using `id` parameter as singleton mode
48
final counterById = Rt.singleton(() => Counter(), id: 'CounterById');
6 collapsed lines
49
print("counterById: ${counterById?.hashCode}");
50
51
// Delete the dependency instance and its register using `id` parameter
52
Rt.destroy<Counter>(id: 'CounterById');
53
54
runApp(MyApp());
55
}
13 collapsed lines
56
57
class MyApp extends StatelessWidget {
58
@override
59
Widget build(BuildContext context) {
60
return MaterialApp(
61
home: Material(
62
child: Center(
63
child: Text('See the output in the terminal'),
64
),
65
),
66
);
67
}
68
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.get

The Rt.get method creates the dependency instance(if not created yet), retrieves it, and then return it. If the dependency is not created, it will return null .

Syntax

T? Rt.get<T>([String? id]);

Parameters

  • id: An optional identifier for the T dependency. If not provided, the default instance of the T dependency is used.

Example

See the example below to understand how to use the Rt.get method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
3 collapsed lines
6
// Register a dependency
7
Rt.register<Counter>(() => Counter());
8
9
// Get the dependency
10
final counter = Rt.get<Counter>();
5 collapsed lines
11
print("counter: ${counter?.hashCode}");
12
13
// Delete the dependency
14
Rt.delete<Counter>();
15
16
// Can't get the dependency instance, because the dependency register was deleted
17
final counter2 = Rt.get<Counter>();
5 collapsed lines
18
print("counter2: ${counter2?.hashCode}"); // counter2: null
19
20
// Register a dependency
21
Rt.register<Counter>(() => Counter(), id: 'CounterById');
22
23
// Get the dependency using `id` parameter
24
final counterById = Rt.get<Counter>('CounterById');
5 collapsed lines
25
print("counterById: ${counterById?.hashCode}");
26
27
// Delete the dependency using `id` parameter
28
Rt.delete<Counter>('CounterById');
29
30
// Can't get the dependency instance using `id` parameter, because the dependency register was deleted
31
final counterById2 = Rt.get<Counter>('CounterById');
3 collapsed lines
32
print("counterById2: ${counterById2?.hashCode}"); // counter2: null
33
34
runApp(MyApp());
35
}
13 collapsed lines
36
37
class MyApp extends StatelessWidget {
38
@override
39
Widget build(BuildContext context) {
40
return MaterialApp(
41
home: Material(
42
child: Center(
43
child: Text('See the output in the terminal'),
44
),
45
),
46
);
47
}
48
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.find

The Rt.find method retrieves the created dependency instance and returns it. If the dependency is not created, returns null .

Syntax

T? Rt.find<T>([String? id]);

Parameters

  • id: An optional identifier for the T dependency. If not provided, the default instance of the T dependency is used.

Example

See the example below to understand how to use the Rt.find method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
3 collapsed lines
6
// Create a dependency
7
Rt.create<Counter>(() => Counter());
8
9
// Find the dependency
10
final counter = Rt.find<Counter>();
5 collapsed lines
11
print("counter: ${counter?.hashCode}");
12
13
// Delete the dependency
14
Rt.delete<Counter>();
15
16
// Can't find the dependency instance, because the dependency register was deleted
17
final counter2 = Rt.find<Counter>();
5 collapsed lines
18
print("counter2: ${counter2?.hashCode}"); // counter2: null
19
20
// Create a dependency
21
Rt.create<Counter>(() => Counter(), id: 'CounterById');
22
23
// Find the dependency using `id` parameter
24
final counterById = Rt.find<Counter>('CounterById');
5 collapsed lines
25
print("counterById: ${counterById?.hashCode}");
26
27
// Delete the dependency using `id` parameter
28
Rt.delete<Counter>('CounterById');
29
30
// Can't find the dependency instance using `id` parameter, because the dependency register was deleted
31
final counterById2 = Rt.find<Counter>('CounterById');
3 collapsed lines
32
print("counterById2: ${counterById2?.hashCode}"); // counter2: null
33
34
runApp(MyApp());
35
}
13 collapsed lines
36
37
class MyApp extends StatelessWidget {
38
@override
39
Widget build(BuildContext context) {
40
return MaterialApp(
41
home: Material(
42
child: Center(
43
child: Text('See the output in the terminal'),
44
),
45
),
46
);
47
}
48
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.exists

The Rt.exists method checks if the dependency instance exists in Reactter. It returns true if the dependency exists, otherwise return false .

Syntax

bool Rt.exists<T>([String? id]);

Parameters

  • id: An optional identifier for the T dependency. If not provided, the default instance of the T dependency is used.

Example

See the example below to understand how to use the Rt.exists method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
3 collapsed lines
6
// Create a dependency
7
Rt.create<Counter>(() => Counter());
8
9
// Check if the dependency exists
10
final isExists = Rt.exists<Counter>();
5 collapsed lines
11
print("Counter exists: $isExists"); // Counter exists: true
12
13
// Delete the dependency
14
Rt.delete<Counter>();
15
16
// Check if the dependency exists after deleting it
17
final isExists2 = Rt.exists<Counter>();
7 collapsed lines
18
print(
19
"Counter exists after deleting: $isExists2",
20
); // Counter exists after deleting: false
21
22
// Create a dependency using `id` parameter
23
Rt.create<Counter>(() => Counter(), id: 'CounterById');
24
25
// Check if the dependency exists using `id` parameter
26
final isExistsById = Rt.exists<Counter>('CounterById');
5 collapsed lines
27
print("Counter by id exists: $isExistsById"); // Counter by id exists: true
28
29
// Delete the dependency using `id` parameter
30
Rt.delete<Counter>('CounterById');
31
32
// Check if the dependency exists after deleting it using `id` parameter
33
final isExistsById2 = Rt.exists<Counter>('CounterById');
5 collapsed lines
34
print(
35
"Counter by id exists after deleting: $isExistsById2",
36
); // Counter by id exists after deleting: false
37
38
runApp(MyApp());
39
}
13 collapsed lines
40
41
class MyApp extends StatelessWidget {
42
@override
43
Widget build(BuildContext context) {
44
return MaterialApp(
45
home: Material(
46
child: Center(
47
child: Text('See the output in the terminal'),
48
),
49
),
50
);
51
}
52
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.getDependencyMode

The Rt.getDependencyMode method retrieves the dependency mode of a registered dependency. If the dependency is not registered, it returns null .

Syntax

DependencyMode? Rt.getDependencyMode<T>([String? id]);

Parameters

  • id: An optional identifier for the T dependency. If not provided, the default instance of the T dependency is used.

Example

See the example below to understand how to use the Rt.getDependencyMode method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Create the dependencies with different dependency modes
7
final counter = Counter();
8
final counter2 = Rt.builder<Counter>(() => Counter(), id: 'Builder');
9
final counter3 = Rt.factory<Counter>(() => Counter(), id: 'Factory');
10
final counter4 = Rt.singleton<Counter>(
11
() => Counter(),
12
id: 'Singleton',
13
);
14
15
// Check the dependency modes
16
final counterMode = Rt.getDependencyMode(counter);
17
final counter2Mode = Rt.getDependencyMode(counter2);
18
final counter3Mode = Rt.getDependencyMode(counter3);
19
final counter4Mode = Rt.getDependencyMode(counter4);
15 collapsed lines
20
21
print(
22
'Counter mode: $counterMode',
23
); // Counter mode: null
24
print(
25
'Counter2 mode: $counter2Mode',
26
); // Counter2 mode: DependencyMode.builder
27
print(
28
'Counter3 mode: $counter3Mode',
29
); // Counter3 mode: DependencyMode.factory
30
print(
31
'Counter4 mode: $counter4Mode',
32
); // Counter4 mode: DependencyMode.singleton
33
34
runApp(MyApp());
35
}
13 collapsed lines
36
37
class MyApp extends StatelessWidget {
38
@override
39
Widget build(BuildContext context) {
40
return MaterialApp(
41
home: Material(
42
child: Center(
43
child: Text('See the output in the terminal'),
44
),
45
),
46
);
47
}
48
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.delete

The Rt.delete method removes the dependency instance and its registration from the Reactter based on the dependency mode. It returns true if the dependency is successfully deleted, otherwise return false .

Syntax

bool Rt.delete<T>([String? id]);

Parameters

  • id: An optional identifier for the T dependency. If not provided, the default instance of the T dependency is used.

Example

See the example below to understand how to use the Rt.delete method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Create the dependencies with different dependency modes
7
final counter = Rt.create<Counter>(() => Counter());
8
final counter2 = Rt.builder<Counter>(() => Counter(), id: 'Builder');
9
final counter3 = Rt.factory<Counter>(() => Counter(), id: 'Factory');
10
final counter4 = Rt.singleton<Counter>(
11
() => Counter(),
12
id: 'Singleton',
13
);
14
15
// Delete the dependencies
16
final isDeleted = Rt.delete<Counter>();
17
final isDeleted2 = Rt.delete<Counter>('Builder');
18
final isDeleted3 = Rt.delete<Counter>('Factory');
19
final isDeleted4 = Rt.delete<Counter>('Singleton');
38 collapsed lines
20
21
// Print if the dependencies are deleted
22
print('isDeleted: $isDeleted'); // isDeleted: true
23
print('isDeleted2: $isDeleted2'); // isDeleted2: true
24
print('isDeleted3: $isDeleted3'); // isDeleted3: true
25
print('isDeleted4: $isDeleted4'); // isDeleted4: false
26
27
// Get the dependencies
28
final counterAfterDeleted = Rt.get<Counter>();
29
final counterAfterDeleted2 = Rt.get<Counter>('Builder');
30
final counterAfterDeleted3 = Rt.get<Counter>('Factory');
31
final counterAfterDeleted4 = Rt.get<Counter>('Singleton');
32
33
// Check if the dependencies are obtained
34
final isObtained = counterAfterDeleted != null;
35
final isObtained2 = counterAfterDeleted2 != null;
36
final isObtained3 = counterAfterDeleted3 != null;
37
final isObtained4 = counterAfterDeleted4 != null;
38
39
// Print if the dependencies are obtained
40
print('isObtained: $isObtained'); // isObtained: false
41
print('isObtained2: $isObtained2'); // isObtained2: false
42
print('isObtained3: $isObtained3'); // isObtained3: true
43
print('isObtained4: $isObtained4'); // isObtained4: true
44
45
// Check if the dependencies are the same instance
46
final isSameInstance = counter == counterAfterDeleted;
47
final isSameInstance2 = counter2 == counterAfterDeleted2;
48
final isSameInstance3 = counter3 == counterAfterDeleted3;
49
final isSameInstance4 = counter4 == counterAfterDeleted4;
50
51
// Print if the dependencies are the same instance
52
print('isSameInstance: $isSameInstance'); // isSameInstance: false
53
print('isSameInstance2: $isSameInstance2'); // isSameInstance2: false
54
print('isSameInstance3: $isSameInstance3'); // isSameInstance3: false
55
print('isSameInstance4: $isSameInstance4'); // isSameInstance4: true
56
57
runApp(MyApp());
58
}
13 collapsed lines
59
60
class MyApp extends StatelessWidget {
61
@override
62
Widget build(BuildContext context) {
63
return MaterialApp(
64
home: Material(
65
child: Center(
66
child: Text('See the output in the terminal'),
67
),
68
),
69
);
70
}
71
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.destroy

The Rt.destroy method forcibly removes the dependency instance and its registration from the Reactter. It returns true if the dependency is successfully destroyed, otherwise return false .

Syntax

bool Rt.destroy<T>({
String? id,
bool onlyDependency = false,
});

Parameters

  • id: An optional identifier for the T dependency. If not provided, the default instance of the T dependency is used.
  • onlyDependency: An optional parameter to delete the dependency instance only. The default value is false .

Example

See the example below to understand how to use the Rt.destroy method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Create the dependencies with different dependency modes
7
Rt.create<Counter>(() => Counter());
8
Rt.builder<Counter>(() => Counter(), id: 'Builder');
9
Rt.factory<Counter>(() => Counter(), id: 'Factory');
10
Rt.singleton<Counter>(() => Counter(), id: 'Singleton');
11
12
// Delete the dependencies
13
final isDestroyed = Rt.destroy<Counter>(onlyInstance: true);
14
final isDestroyed2 = Rt.destroy<Counter>(id: 'Builder');
15
final isDestroyed3 = Rt.destroy<Counter>(id: 'Factory');
16
final isDestroyed4 = Rt.destroy<Counter>(id: 'Singleton');
26 collapsed lines
17
18
// Print if the dependencies are destroyed
19
print('isDestroyed: $isDestroyed'); // isDestroyed: false
20
print('isDestroyed2: $isDestroyed2'); // isDestroyed2: true
21
print('isDestroyed3: $isDestroyed3'); // isDestroyed3: true
22
print('isDestroyed4: $isDestroyed4'); // isDestroyed4: true
23
24
// Get the dependencies
25
final counterAfterDeleted = Rt.get<Counter>();
26
final counterAfterDeleted2 = Rt.get<Counter>('Builder');
27
final counterAfterDeleted3 = Rt.get<Counter>('Factory');
28
final counterAfterDeleted4 = Rt.get<Counter>('Singleton');
29
30
// Check if the dependencies are obtained
31
final isObtained = counterAfterDeleted != null;
32
final isObtained2 = counterAfterDeleted2 != null;
33
final isObtained3 = counterAfterDeleted3 != null;
34
final isObtained4 = counterAfterDeleted4 != null;
35
36
// Print if the dependencies are obtained
37
print('isObtained: $isObtained'); // isObtained: true
38
print('isObtained2: $isObtained2'); // isObtained2: false
39
print('isObtained3: $isObtained3'); // isObtained3: false
40
print('isObtained4: $isObtained4'); // isObtained4: false
41
42
runApp(MyApp());
43
}
13 collapsed lines
44
45
class MyApp extends StatelessWidget {
46
@override
47
Widget build(BuildContext context) {
48
return MaterialApp(
49
home: Material(
50
child: Center(
51
child: Text('See the output in the terminal'),
52
),
53
),
54
);
55
}
56
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.unregister

The Rt.unregister method is used to unregister the dependency from the Reactter. It returns true if the dependency is unregistered successfully, otherwise return false .

Syntax

bool Rt.unregister<T>([String? id]);

Parameters

  • id: An optional identifier for the T dependency. If not provided, the method will attempt to unregister the default instance of the dependency.

Example

See the example below to understand how to use the Rt.unregister method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Create the dependencies with different dependency modes
7
Rt.create<Counter>(() => Counter());
8
Rt.builder<Counter>(() => Counter(), id: 'Builder');
9
Rt.factory<Counter>(() => Counter(), id: 'Factory');
10
Rt.singleton<Counter>(() => Counter(), id: 'Singleton');
11
12
// Unregister the dependencies
13
final isUnregistered = Rt.unregister<Counter>();
14
final isUnregistered2 = Rt.unregister<Counter>('Builder');
15
final isUnregistered3 = Rt.unregister<Counter>('Factory');
16
final isUnregistered4 = Rt.unregister<Counter>('Singleton');
7 collapsed lines
17
18
// All should return false, because the dependencies has an active instance
19
print('isUnregistered: $isUnregistered'); // isUnregistered: false
20
print('isUnregistered2: $isUnregistered2'); // isUnregistered2: false
21
print('isUnregistered3: $isUnregistered3'); // isUnregistered3: false
22
print('isUnregistered4: $isUnregistered4'); // isCounterUnregistered4: false
23
24
// Delete the dependencies
25
Rt.destroy<Counter>(onlyInstance: true);
26
Rt.delete<Counter>('Builder');
27
Rt.delete<Counter>('Factory');
28
Rt.delete<Counter>('Singleton');
29
30
// Unregister the dependencies after deleted
31
final isUnregisteredAfterDeleted = Rt.unregister<Counter>();
32
final isUnregisteredAfterDeleted2 = Rt.unregister<Counter>('Builder');
33
final isUnregisteredAfterDeleted3 = Rt.unregister<Counter>('Factory');
34
final isUnregisteredAfterDeleted4 = Rt.unregister<Counter>('Singleton');
18 collapsed lines
35
36
// These should return true, because the instance has been deleted but the dependency is still registered
37
print(
38
'isUnregisteredAfterDeleted: $isUnregisteredAfterDeleted',
39
); // isUnregisteredAfterDeleted: true
40
print(
41
'isUnregisteredAfterDeleted3: $isUnregisteredAfterDeleted3',
42
); // isUnregisteredAfterDeleted3: true
43
44
// These should return false, because the instance and its registration has been deleted
45
print(
46
'isUnregisteredAfterDeleted2: $isUnregisteredAfterDeleted2',
47
); // isUnregisteredAfterDeleted2: false
48
print(
49
'isUnregisteredAfterDeleted4: $isUnregisteredAfterDeleted4',
50
); // isUnregisteredAfterDeleted4: false
51
52
runApp(MyApp());
53
}
13 collapsed lines
54
55
class MyApp extends StatelessWidget {
56
@override
57
Widget build(BuildContext context) {
58
return MaterialApp(
59
home: Material(
60
child: Center(
61
child: Text('See the output in the terminal'),
62
),
63
),
64
);
65
}
66
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}

Rt.isActive

The Rt.isActive method is used to check if the dependency is registered in Reactter. It returns true if the dependency is registered, otherwise return false .

Syntax

bool Rt.isActive<T>([String? id]);

Parameters

  • id: An optional identifier for the T dependency. If not provided, the method will attempt to check the default instance of the dependency.

Example

See the example below to understand how to use the Rt.isActive method in different scenarios.

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'counter.dart';
4
5
void main() {
6
// Create the dependencies with different dependency modes
7
final counter = Counter();
8
final counter2 = Rt.builder<Counter>(() => Counter(), id: 'Builder');
9
final counter3 = Rt.factory<Counter>(() => Counter(), id: 'Factory');
10
final counter4 = Rt.singleton<Counter>(
11
() => Counter(),
12
id: 'Singleton',
13
);
14
15
// Check if the instances are registered
16
final isActive = Rt.isActive(counter);
17
final isActive2 = Rt.isActive(counter2);
18
final isActive3 = Rt.isActive(counter3);
19
final isActive4 = Rt.isActive(counter4);
7 collapsed lines
20
21
// Print if the instances are registered
22
print('isActive: $isActive'); // isActive: false
23
print('isActive2: $isActive2'); // isActive2: true
24
print('isActive3: $isActive3'); // isActive3: true
25
print('isActive4: $isActive4'); // isActive4: true
26
27
// Delete the dependencies
28
Rt.destroy<Counter>(onlyInstance: true);
29
Rt.delete<Counter>('Builder');
30
Rt.delete<Counter>('Factory');
31
Rt.delete<Counter>('Singleton');
32
33
// Check if the instances are registered
34
final isActiveAfterDeleted = Rt.isActive(counter);
35
final isActiveAfterDeleted2 = Rt.isActive(counter2);
36
final isActiveAfterDeleted3 = Rt.isActive(counter3);
37
final isActiveAfterDeleted4 = Rt.isActive(counter4);
16 collapsed lines
38
39
// Print if the instances are registere after deleted
40
print(
41
'isActiveAfterDeleted: $isActiveAfterDeleted',
42
); // isActiveAfterDeleted: false
43
print(
44
'isActiveAfterDeleted2: $isActiveAfterDeleted2',
45
); // isActiveAfterDeleted2: false
46
print(
47
'isActiveAfterDeleted3: $isActiveAfterDeleted3',
48
); // isActiveAfterDeleted3: false
49
print(
50
'isActiveAfterDeleted4: $isActiveAfterDeleted4',
51
); // isActiveAfterDeleted4: true
52
53
runApp(MyApp());
54
}
13 collapsed lines
55
56
class MyApp extends StatelessWidget {
57
@override
58
Widget build(BuildContext context) {
59
return MaterialApp(
60
home: Material(
61
child: Center(
62
child: Text('See the output in the terminal'),
63
),
64
),
65
);
66
}
67
}
1
import 'package:reactter/reactter.dart';
2
3
class Counter {
4
// Create a reactive state using the `Signal` class
5
final count = Signal(0);
6
7
void increment() {
8
count.value++;
9
}
10
11
void decrement() {
12
count.value--;
13
}
14
}