Saltearse al contenido

Event Handler Methods

Esta página aún no está disponible en tu idioma.

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

Rt.on

The Rt.on method listens for an event emitted by an instance, allowing the listener to respond whenever the event occurs.

Syntax

void on<T, P>(
Object? instance,
Enum eventName,
Functioon(T instance, P param) callback,
);

Parameters

  • instance: The T instance to listen to.
  • event: The Enum event to listen to.
  • callback : The function to execute when the event is emitted. The function should accept two parameters:
    • instance: The T instance that emitted the event.
    • param: The P parameter passed when the event is emitted.

Example

See the example below to understand how to use the Rt.on method:

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'my_dependency.dart';
4
5
void main() {
6
// Listen to the `myEvent` event of the `MyDependency` before it's created.
7
Rt.on(
8
RtDependency<MyDependency>(),
9
CustomEvent.myEvent,
10
(instance, param) => print('CustomEvent emitted with param: $param'),
11
);
12
13
// Create a new instance of `MyDependency`.
14
final myDependency = Rt.create(() => MyDependency())!;
15
16
// Listen to the `didUpdate` event of the `MyDependency` instance.
17
Rt.on<MyDependency, RtState>(
18
myDependency,
19
Lifecycle.didUpdate,
20
(instance, state) => print('The state updated is: ${state.runtimeType}'),
21
);
22
23
// Listen to the `didUpdate` event of the `stateA`.
24
Rt.on(
25
myDependency.stateA,
26
Lifecycle.didUpdate,
27
(instance, Signal state) => print('stateA updated: ${state.value}'),
28
);
29
30
// Listen to the `didUpdate` event of the `stateB`.
31
Rt.on(
32
myDependency.stateB,
33
Lifecycle.didUpdate,
34
(instance, Signal state) => print('stateB updated: ${state.value}'),
35
);
35 collapsed lines
36
37
// Change the value of `stateA` to `10`.
38
// Print:
39
// stateA updated: 10
40
// The state updated is: Signal<int>
41
myDependency.stateA.value = 10;
42
43
// Change the value of `stateB` to `'Hello World!'`.
44
// Print:
45
// stateB updated: 'Hello World!'
46
// The state updated is: Signal<String>
47
myDependency.stateB.value = 'Hello World!';
48
49
// Emit the `myEvent` event with the parameter `test`.
50
// Print:
51
// CustomEvent.myEvent emitted with param: test
52
Rt.emit(myDependency, CustomEvent.myEvent, 'test');
53
54
// Delete the `MyDependency` instance
55
Rt.delete<MyDependency>();
56
57
// Cannot listen to the `didUpdate` event of the `MyDependency` instance
58
// because it's deleted. This will throw an error.
59
// myDependency.stateA.value = 20;
60
61
// Cannot listen to the `myEvent` event using the `MyDependency` instance
62
// because it's deleted.
63
Rt.emit(myDependency, CustomEvent.myEvent, 'This is not printed');
64
65
// Can still emit to the `myEvent` event using the `RtDependency`
66
// Print:
67
// CustomEvent.myEvent emitted with param: 42
68
Rt.emit(RtDependency<MyDependency>(), CustomEvent.myEvent, 42);
69
70
runApp(MyApp());
71
}
13 collapsed lines
72
73
class MyApp extends StatelessWidget {
74
@override
75
Widget build(BuildContext context) {
76
return MaterialApp(
77
home: Material(
78
child: Center(
79
child: Text('See the output in the terminal'),
80
),
81
),
82
);
83
}
84
}
1
import 'package:reactter/reactter.dart';
2
3
enum CustomEvent { myEvent }
4
5
class MyDependency {
6
final stateA = Signal(0);
7
final stateB = Signal('InitialValue');
8
}

Rt.one

The Rt.one method listens for an event emitted by an instance, but only once. After the event is triggered, the listener is automatically removed.

Syntax

void one<T, P>(
Object? instance,
Enum eventName,
Functioon(T instance, P param) callback,
);

Parameters

  • instance: The T instance to listen to.
  • event: The Enum event to listen to.
  • callback : The function to execute when the event is emitted. The function should accept two parameters:
    • instance: The T instance that emitted the event.
    • param: The P parameter passed when the event is emitted.

Example

See the example below to understand how to use the Rt.one method:

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'my_dependency.dart';
4
5
void main() {
6
// Listen to the `myEvent` event of the `MyDependency` before it's created.
7
Rt.one(
8
RtDependency<MyDependency>(),
9
CustomEvent.myEvent,
10
(instance, param) => print('CustomEvent emitted with param: $param'),
11
);
12
13
// Create a new instance of `MyDependency`.
14
final myDependency = Rt.create(() => MyDependency())!;
15
16
// Listen to the `didUpdate` event of the `MyDependency` instance.
17
Rt.one<MyDependency, RtState>(
18
myDependency,
19
Lifecycle.didUpdate,
20
(instance, state) => print('The state updated is: ${state.runtimeType}'),
21
);
22
23
// Listen to the `didUpdate` event of the `stateA`.
24
Rt.one(
25
myDependency.stateA,
26
Lifecycle.didUpdate,
27
(instance, Signal state) => print('stateA updated: ${state.value}'),
28
);
29
30
// Listen to the `didUpdate` event of the `stateB`.
31
Rt.one(
32
myDependency.stateB,
33
Lifecycle.didUpdate,
34
(instance, Signal state) => print('stateB updated: ${state.value}'),
35
);
31 collapsed lines
36
37
// Change the value of `stateA` to `10`.
38
// Print:
39
// stateA updated: 10
40
// The state updated is: Signal<int>
41
myDependency.stateA.value = 10;
42
43
// Change the value of `stateB` to `'Hello World!'`.
44
// No print for the `didUpdate` event of `MyDependency` instance
45
// because it's a one-time listener.
46
// Print:
47
// stateB updated: 'Hello World!'
48
myDependency.stateB.value = 'Hello World!';
49
50
// Emit the `myEvent` event with the parameter `test`.
51
// Print:
52
// CustomEvent.myEvent emitted with param: test
53
Rt.emit(myDependency, CustomEvent.myEvent, 'test');
54
55
// Delete the `MyDependency` instance
56
Rt.delete<MyDependency>();
57
58
// Cannot listen to the `didUpdate` event of the `MyDependency` instance
59
// because it's deleted. This will throw an error.
60
// myDependency.stateA.value = 20;
61
62
/// Cannot listen to the `myEvent` event using the `MyDependency` instance
63
/// because the listener is one-time.
64
Rt.emit(RtDependency<MyDependency>(), CustomEvent.myEvent, 42);
65
66
runApp(MyApp());
67
}
13 collapsed lines
68
69
class MyApp extends StatelessWidget {
70
@override
71
Widget build(BuildContext context) {
72
return MaterialApp(
73
home: Material(
74
child: Center(
75
child: Text('See the output in the terminal'),
76
),
77
),
78
);
79
}
80
}
1
import 'package:reactter/reactter.dart';
2
3
enum CustomEvent { myEvent }
4
5
class MyDependency {
6
final stateA = Signal(0);
7
final stateB = Signal('InitialValue');
8
}

Rt.emit

The Rt.emit method triggers an event from an instance, notifying all listeners of that event.

Syntax

void emit(
Object? instance,
Enum eventName,
dynamic param,
);

Parameters

  • instance: The instance to emit the event.
  • event: The Enum event to emit.
  • param: The parameter to pass when emitting the event.

Example

See the example below to understand how to use the Rt.emit method:

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'my_dependency.dart';
4
5
void main() {
43 collapsed lines
6
// Listen to the `myEvent` event of the `MyDependency` before it's created.
7
Rt.on(
8
RtDependency<MyDependency>(),
9
CustomEvent.myEvent,
10
(instance, param) => print('CustomEvent emitted with param: $param'),
11
);
12
13
// Create a new instance of `MyDependency`.
14
final myDependency = Rt.create(() => MyDependency())!;
15
16
// Listen to the `didUpdate` event of the `MyDependency` instance.
17
Rt.on<MyDependency, RtState>(
18
myDependency,
19
Lifecycle.didUpdate,
20
(instance, state) => print('The state updated is: ${state.runtimeType}'),
21
);
22
23
// Listen to the `didUpdate` event of the `stateA`.
24
Rt.on(
25
myDependency.stateA,
26
Lifecycle.didUpdate,
27
(instance, Signal state) => print('stateA updated: ${state.value}'),
28
);
29
30
// Listen to the `didUpdate` event of the `stateB`.
31
Rt.on(
32
myDependency.stateB,
33
Lifecycle.didUpdate,
34
(instance, Signal state) => print('stateB updated: ${state.value}'),
35
);
36
37
// Change the value of `stateA` to `10`.
38
// Print:
39
// stateA updated: 10
40
// The state updated is: Signal<int>
41
myDependency.stateA.value = 10;
42
43
// Change the value of `stateB` to `'Hello World!'`.
44
// Print:
45
// stateB updated: 'Hello World!'
46
// The state updated is: Signal<String>
47
myDependency.stateB.value = 'Hello World!';
48
49
// Emit the `myEvent` event with the parameter `test`.
50
// Print:
51
// CustomEvent.myEvent emitted with param: test
52
Rt.emit(myDependency, CustomEvent.myEvent, 'test');
8 collapsed lines
53
54
// Delete the `MyDependency` instance
55
Rt.delete<MyDependency>();
56
57
// Cannot listen to the `didUpdate` event of the `MyDependency` instance
58
// because it's deleted. This will throw an error.
59
// myDependency.stateA.value = 20;
60
61
// Cannot listen to the `myEvent` event using the `MyDependency` instance
62
// because it's deleted.
63
Rt.emit(myDependency, CustomEvent.myEvent, 'This is not printed');
64
65
// Can still emit to the `myEvent` event using the `RtDependency`
66
// Print:
67
// CustomEvent.myEvent emitted with param: 42
68
Rt.emit(RtDependency<MyDependency>(), CustomEvent.myEvent, 42);
2 collapsed lines
69
70
runApp(MyApp());
71
}
13 collapsed lines
72
73
class MyApp extends StatelessWidget {
74
@override
75
Widget build(BuildContext context) {
76
return MaterialApp(
77
home: Material(
78
child: Center(
79
child: Text('See the output in the terminal'),
80
),
81
),
82
);
83
}
84
}
1
import 'package:reactter/reactter.dart';
2
3
enum CustomEvent { myEvent }
4
5
class MyDependency {
6
final stateA = Signal(0);
7
final stateB = Signal('InitialValue');
8
}

Rt.off

The Rt.off method removes a specific event listener from an instance, stopping it from receiving further notifications for that event.

Syntax

void off<T, P>(
Object? instance,
Enum eventName,
Functioon(T instance, P param) callback,
);

Parameters

  • instance: The T instance to stop listening to.
  • event: The Enum event to stop listening to.
    • callback : The function to stop executing the callback. The function should accept two parameters:
    • instance: The T instance that emitted the event.
    • param: The P parameter passed when the event is emitted.

Example

See the example below to understand how to use the Rt.off method:

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'my_dependency.dart';
17 collapsed lines
4
5
void onMyEvent(instance, param) {
6
print('CustomEvent emitted with param: $param');
7
}
8
9
void onDidUpdate(instance, state) {
10
print('The state updated is: ${state.runtimeType}');
11
}
12
13
void onDidUpdateStateA(instance, Signal state) {
14
print('stateA updated: ${state.value}');
15
}
16
17
void onDidUpdateStateB(instance, Signal state) {
18
print('stateB updated: ${state.value}');
19
}
20
21
void main() {
40 collapsed lines
22
// Listen to the `myEvent` event of the `MyDependency` before it's created.
23
Rt.on(
24
RtDependency<MyDependency>(),
25
CustomEvent.myEvent,
26
onMyEvent,
27
);
28
29
// Create a new instance of `MyDependency`.
30
final myDependency = Rt.create(() => MyDependency())!;
31
32
// Listen to the `didUpdate` event of the `MyDependency` instance.
33
Rt.on<MyDependency, RtState>(
34
myDependency,
35
Lifecycle.didUpdate,
36
onDidUpdate,
37
);
38
39
// Listen to the `didUpdate` event of the `stateA`.
40
Rt.on(myDependency.stateA, Lifecycle.didUpdate, onDidUpdateStateA);
41
42
// Listen to the `didUpdate` event of the `stateB`.
43
Rt.on(myDependency.stateB, Lifecycle.didUpdate, onDidUpdateStateB);
44
45
// Change the value of `stateA` to `10`.
46
// Print:
47
// stateA updated: 10
48
// The state updated is: Signal<int>
49
myDependency.stateA.value = 10;
50
51
// Change the value of `stateB` to `'Hello World!'`.
52
// Print:
53
// stateB updated: 'Hello World!'
54
// The state updated is: Signal<String>
55
myDependency.stateB.value = 'Hello World!';
56
57
// Emit the `myEvent` event with the parameter `test`.
58
// Print:
59
// CustomEvent.myEvent emitted with param: test
60
Rt.emit(myDependency, CustomEvent.myEvent, 'test');
61
62
// Stop listening to the `myEvent` event of the `MyDependency`
63
Rt.off(
64
RtDependency<MyDependency>(),
65
CustomEvent.myEvent,
66
onMyEvent,
67
);
68
69
// Stop listening to the `didUpdate` event of the `MyDependency` instance
70
Rt.off(myDependency, Lifecycle.didUpdate, onDidUpdate);
71
72
// Stop listening to the `didUpdate` event of the `stateA`
73
Rt.off(myDependency.stateA, Lifecycle.didUpdate, onDidUpdateStateA);
10 collapsed lines
74
75
// No print for neither `stateA` nor the `MyDependency` instance
76
// because the listeners are removed
77
myDependency.stateA.value = 20;
78
79
// Cannot listen to the `myEvent` event using the `MyDependency` instance
80
// because the listener is removed
81
Rt.emit(myDependency, CustomEvent.myEvent, 'This is not printed');
82
83
runApp(MyApp());
84
}
13 collapsed lines
85
86
class MyApp extends StatelessWidget {
87
@override
88
Widget build(BuildContext context) {
89
return MaterialApp(
90
home: Material(
91
child: Center(
92
child: Text('See the output in the terminal'),
93
),
94
),
95
);
96
}
97
}
1
import 'package:reactter/reactter.dart';
2
3
enum CustomEvent { myEvent }
4
5
class MyDependency {
6
final stateA = Signal(0);
7
final stateB = Signal('InitialValue');
8
}

Rt.offAll

The Rt.offAll method removes all event listeners for a specific event from an instance, ensuring no listeners are notified when the event is triggered in the future.

Syntax

void offAll<T>(Object? instance);

Parameters

  • instance: The T instance to stop listening to all events.

Example

See the example below to understand how to use the Rt.offAll method:

1
import 'package:flutter/material.dart';
2
import 'package:reactter/reactter.dart';
3
import 'my_dependency.dart';
4
5
void main() {
48 collapsed lines
6
// Listen to the `myEvent` event of the `MyDependency` before it's created.
7
Rt.on(
8
RtDependency<MyDependency>(),
9
CustomEvent.myEvent,
10
(instance, param) => print('CustomEvent emitted with param: $param'),
11
);
12
13
// Create a new instance of `MyDependency`.
14
final myDependency = Rt.create(() => MyDependency())!;
15
16
// Listen to the `didUpdate` event of the `MyDependency` instance.
17
Rt.on<MyDependency, RtState>(
18
myDependency,
19
Lifecycle.didUpdate,
20
(instance, state) => print('The state updated is: ${state.runtimeType}'),
21
);
22
23
// Listen to the `didUpdate` event of the `stateA`.
24
Rt.on(
25
myDependency.stateA,
26
Lifecycle.didUpdate,
27
(instance, Signal state) => print('stateA updated: ${state.value}'),
28
);
29
30
// Listen to the `didUpdate` event of the `stateB`.
31
Rt.on(
32
myDependency.stateB,
33
Lifecycle.didUpdate,
34
(instance, Signal state) => print('stateB updated: ${state.value}'),
35
);
36
37
// Change the value of `stateA` to `10`.
38
// Print:
39
// stateA updated: 10
40
// The state updated is: Signal<int>
41
myDependency.stateA.value = 10;
42
43
// Change the value of `stateB` to `'Hello World!'`.
44
// Print:
45
// stateB updated: 'Hello World!'
46
// The state updated is: Signal<String>
47
myDependency.stateB.value = 'Hello World!';
48
49
// Emit the `myEvent` event with the parameter `test`.
50
// Print:
51
// CustomEvent.myEvent emitted with param: test
52
Rt.emit(myDependency, CustomEvent.myEvent, 'test');
53
54
// Remove all event listeners of `stateA`.
55
Rt.offAll(myDependency.stateA);
56
57
// Remove all event listeners of `myDependency`,
58
// including the generic listeners (using `RtDependency`).
59
Rt.offAll(myDependency, true);
16 collapsed lines
60
61
// No print for neither `stateA` nor the `MyDependency` instance
62
// because the listeners are removed
63
myDependency.stateA.value = 20;
64
65
// Change the value of `stateB` to `Hey you!'`.
66
// Only print for the `didUpdate` event of `stateB`.
67
// Print:
68
// stateB updated: 'Hey you!'
69
myDependency.stateB.value = 'Hey you!';
70
71
// Cannot listen to the `myEvent` event using the `MyDependency` instance
72
// because the listeners are removed.
73
Rt.emit(myDependency, CustomEvent.myEvent, 'This is not printed');
74
75
runApp(MyApp());
76
}
13 collapsed lines
77
78
class MyApp extends StatelessWidget {
79
@override
80
Widget build(BuildContext context) {
81
return MaterialApp(
82
home: Material(
83
child: Center(
84
child: Text('See the output in the terminal'),
85
),
86
),
87
);
88
}
89
}
1
import 'package:reactter/reactter.dart';
2
3
enum CustomEvent { myEvent }
4
5
class MyDependency {
6
final stateA = Signal(0);
7
final stateB = Signal('InitialValue');
8
}