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.
Reactter.on
The Reactter.on
method listens for an event emitted by an instance, allowing the listener to respond whenever the event occurs.
Syntax
1void on<T, P>(2 Object? instance,3 Enum eventName,4 Functioon(T instance, P param) callback,5);
Parameters
instance
: TheT
instance to listen to.event
: TheEnum
event to listen to.-
callback
: The function to execute when the event is emitted. The function should accept two parameters:instance
: TheT
instance that emitted the event.param
: TheP
parameter passed when the event is emitted.
Example
See the example below to understand how to use the Reactter.on
method:
8 collapsed lines
1import 'package:reactter/reactter.dart';2
3enum CustomEvent { myEvent }4
5class MyController {6 final stateA = Signal(0);7 final stateB = Signal('InitialValue');8}
1import 'package:reactter/reactter.dart';2import './my_controller.dart';3
4void main() {5 // Listen to the `myEvent` event of the `MyController` instance before it's created6 Reactter.on(7 ReactterDependency<MyController>,8 CustomEvent.myEvent,9 (MyController instance, int param) {10 print('Event emitted with param: $param');11 },12 );4 collapsed lines
13
14 // Create a new instance of `MyController`15 final myController = Reactter.create(() => MyController())!;16
17 // Listen to the `didUpdate` event of the `MyController` instance18 Reactter.on<MyController, ReactterState>(19 myController,20 Lifecycle.didUpdate,21 (instance, state) => print('State: ${state.runtimeType}'),22 );23
24 // Listen to the `didUpdate` event of the `stateA`25 Reactter.on(26 myController.stateA,27 Lifecycle.didUpdate,28 (MyController instance, Signal state) => print('State A updated: ${state.value}'),29 );30
31 // Listen to the `didUpdate` event of the `stateB`32 Reactter.on(33 myController.stateB,34 Lifecycle.didUpdate,35 (MyController instance, Signal state) => print('State B updated: ${state.value}'),36 );28 collapsed lines
37
38 // Change the value of `stateA` to `10`39 // Print:40 // State: Signal<int>41 // State A updated: 1042 myController.stateA.value = 10;43
44 // Change the value of `stateB` to `'Hello World!'`45 // Print:46 // State: Signal<String>47 // State B updated: 'Hello World!'48 myController.stateB.value = 'Hello World!';49
50 // Emit the `myEvent` event with the parameter `42`51 // Print: Event emitted with param: 4252 Reactter.emit(ReactterDependency<MyController>, CustomEvent.myEvent, 42);53
54 // Delete the `MyController`55 Reactter.delete<MyController>();56
57 // Can't change the value of `stateA`58 // Error: "Can't update when it's been disposed"59 // myController.stateA.value = 20;60
61 // The `myEvent` event is not emitted,62 // because the `MyController` together to the listeners were deleted.63 Reactter.emit(ReactterDependency<MyController>, CustomEvent.myEvent, "test");64}
Reactter.one
The Reactter.one
method listens for an event emitted by an instance, but only once. After the event is triggered, the listener is automatically removed.
Syntax
1void one<T, P>(2 Object? instance,3 Enum eventName,4 Functioon(T instance, P param) callback,5);
Parameters
instance
: TheT
instance to listen to.event
: TheEnum
event to listen to.-
callback
: The function to execute when the event is emitted. The function should accept two parameters:instance
: TheT
instance that emitted the event.param
: TheP
parameter passed when the event is emitted.
Example
See the example below to understand how to use the Reactter.one
method:
1import 'package:reactter/reactter.dart';2import './my_controller.dart';3
4void main() {5 // Listen to the `myEvent` event of the `MyController` instance before it's created6 Reactter.one(7 ReactterDependency<MyController>,8 CustomEvent.myEvent,9 (MyController instance, int param) {10 print('Event emitted with param: $param');11 },12 );4 collapsed lines
13
14 // Create a new instance of `MyController`15 final myController = Reactter.create(() => MyController())!;16
17 // Listen to the `didUpdate` event of the `MyController` instance18 Reactter.one<MyController, ReactterState>(19 myController,20 Lifecycle.didUpdate,21 (instance, state) => print('State: ${state.runtimeType}'),22 );23
24 // Listen to the `didUpdate` event of the `stateA`25 Reactter.one(26 myController.stateA,27 Lifecycle.didUpdate,28 (MyController instance, Signal state) => print('State A updated: ${state.value}'),29 );30
31 // Listen to the `didUpdate` event of the `stateB`32 Reactter.one(33 myController.stateB,34 Lifecycle.didUpdate,35 (MyController instance, Signal state) => print('State B updated: ${state.value}'),36 );29 collapsed lines
37
38 // Change the value of `stateA` to `10`39 // Print:40 // State: Signal<int>41 // State A updated: 1042 myController.stateA.value = 10;43
44 // Change the value of `stateB` to `'Hello World!'`45 // Print:46 // State B updated: 'Hello World!'47 myController.stateB.value = 'Hello World!';48
49 // Emit the `myEvent` event with the parameter `42`50 // Print: Event emitted with param: 4251 Reactter.emit(ReactterDependency<MyController>, CustomEvent.myEvent, 42);52
53 // The `myEvent` event is not listened to again54 Reactter.emit(ReactterDependency<MyController>, CustomEvent.myEvent, 'test');55
56 // Delete the `MyController`57 Reactter.delete<MyController>();58
59 // Can't change the value of `stateA`60 // Error: "Can't update when it's been disposed"61 // myController.stateA.value = 20;62
63 // The `myEvent` event is not listened to again64 Reactter.emit(ReactterDependency<MyController>, CustomEvent.myEvent, "test");65}
Reactter.emit
The Reactter.emit
method triggers an event from an instance, notifying all listeners of that event.
Syntax
1void emit(2 Object? instance,3 Enum eventName,4 dynamic param,5);
Parameters
instance
: The instance to emit the event.event
: TheEnum
event to emit.param
: The parameter to pass when emitting the event.
Example
See the example below to understand how to use the Reactter.emit
method:
8 collapsed lines
1import 'package:reactter/reactter.dart';2
3enum CustomEvent { myEvent }4
5class MyController {6 final stateA = Signal(0);7 final stateB = Signal('InitialValue');8}
1import 'package:reactter/reactter.dart';2import './my_controller.dart';3
4void main() {5 // Listen to the `myEvent` event of the `MyController` instance before it's created6 Reactter.on(7 ReactterDependency<MyController>,8 CustomEvent.myEvent,9 (MyController instance, int param) {10 print('Event emitted with param: $param');11 },12 );4 collapsed lines
13
14 // Create a new instance of `MyController`15 final myController = Reactter.create(() => MyController())!;16
17 // Listen to the `didUpdate` event of the `MyController` instance18 Reactter.on<MyController, ReactterState>(19 myController,20 Lifecycle.didUpdate,21 (instance, state) => print('State: ${state.runtimeType}'),22 );23
24 // Listen to the `didUpdate` event of the `stateA`25 Reactter.on(26 myController.stateA,27 Lifecycle.didUpdate,28 (MyController instance, Signal state) => print('State A updated: ${state.value}'),29 );30
31 // Listen to the `didUpdate` event of the `stateB`32 Reactter.on(33 myController.stateB,34 Lifecycle.didUpdate,35 (MyController instance, Signal state) => print('State B updated: ${state.value}'),36 );28 collapsed lines
37
38 // Change the value of `stateA` to `10`39 // Print:40 // State: Signal<int>41 // State A updated: 1042 myController.stateA.value = 10;43
44 // Change the value of `stateB` to `'Hello World!'`45 // Print:46 // State: Signal<String>47 // State B updated: 'Hello World!'48 myController.stateB.value = 'Hello World!';49
50 // Emit the `myEvent` event with the parameter `42`51 // Print: Event emitted with param: 4252 Reactter.emit(ReactterDependency<MyController>, CustomEvent.myEvent, 42);53
54 // Delete the `MyController`55 Reactter.delete<MyController>();56
57 // Can't change the value of `stateA`58 // Error: "Can't update when it's been disposed"59 // myController.stateA.value = 20;60
61 // The `myEvent` event is not emitted,62 // because the `MyController` together to the listeners were deleted.63 Reactter.emit(ReactterDependency<MyController>, CustomEvent.myEvent, "test");64}
Reactter.off
The Reactter.off
method removes a specific event listener from an instance, stopping it from receiving further notifications for that event.
Syntax
1void off<T, P>(2 Object? instance,3 Enum eventName,4 Functioon(T instance, P param) callback,5);
Parameters
instance
: TheT
instance to stop listening to.event
: TheEnum
event to stop listening to.-
-
callback
: The function to stop executing the callback. The function should accept two parameters: instance
: TheT
instance that emitted the event.param
: TheP
parameter passed when the event is emitted.
-
Example
See the example below to understand how to use the Reactter.off
method:
8 collapsed lines
1import 'package:reactter/reactter.dart';2
3enum CustomEvent { myEvent }4
5class MyController {6 final stateA = Signal(0);7 final stateB = Signal('InitialValue');8}
67 collapsed lines
1import 'package:reactter/reactter.dart';2import './my_controller.dart';3
4void onMyEvent(instance, param) {5 print('Event emitted with param: $param');6}7
8void onDidUpdate(instance, state) {9 print('State: ${state.runtimeType}');10}11
12void onDidUpdateStateA(MyController instance, Signal state) {13 print('State A updated: ${state.value}');14}15
16void onDidUpdateStateB(MyController instance, Signal state) {17 print('State B updated: ${state.value}');18}19
20void main() {21 // Listen to the `myEvent` event of the `MyController` instance before it's created22 Reactter.on(23 ReactterDependency<MyController>,24 CustomEvent.myEvent,25 onMyEvent,26 );27
28 // Create a new instance of `MyController`29 final myController = Reactter.create(() => MyController())!;30
31 // Listen to the `didUpdate` event of the `MyController` instance32 Reactter.on<MyController, ReactterState>(33 myController,34 Lifecycle.didUpdate,35 onDidUpdate,36 );37
38 // Listen to the `didUpdate` event of the `stateA`39 Reactter.on(40 myController.stateA,41 Lifecycle.didUpdate,42 onDidUpdateStateA,43 );44
45 // Listen to the `didUpdate` event of the `stateB`46 Reactter.on(47 myController.stateB,48 Lifecycle.didUpdate,49 didUpdateStateBListener,50 );51
52 // Change the value of `stateA` to `10`53 // Print:54 // State: Signal<int>55 // State A updated: 1056 myController.stateA.value = 10;57
58 // Change the value of `stateB` to `'Hello World!'`59 // Print:60 // State: Signal<String>61 // State B updated: 'Hello World!'62 myController.stateB.value = 'Hello World!';63
64 // Emit the `myEvent` event with the parameter `42`65 // Print: Event emitted with param: 4266 Reactter.emit(ReactterDependency<MyController>, CustomEvent.myEvent, 42);67
68 // Stop listening to the `didUpdate` event of the `stateA`69 Reactter.off(70 myController.stateA,71 Lifecycle.didUpdate,72 onDidUpdateStateA,73 );5 collapsed lines
74
75 // Change the value of `stateA` to `20`76 // Print: State: Signal<int>77 myController.stateA.value = 20;78
79 // Stop listening to the `didUpdate` event of the `MyController` instance80 Reactter.off(81 myController,82 Lifecycle.didUpdate,83 onDidUpdate,84 );5 collapsed lines
85
86 // Change the value of `stateB` to `'Hey you!'`87 // Print: State B updated: 'Hey you!'88 myController.stateB.value = 'Hey you!';89
90 // Stop listening to the `myEvent` event of the `MyController` instance91 Reactter.off(92 ReactterDependency<MyController>,93 CustomEvent.myEvent,94 onMyEvent,95 );5 collapsed lines
96
97 // Emit the `myEvent` event with the parameter `test`98 // The `myEvent` event is not listened to again99 Reactter.emit(ReactterDependency<MyController>, CustomEvent.myEvent, 'test');100}
Reactter.offAll
The Reactter.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
1void offAll<T>(Object? instance);
Parameters
instance
: TheT
instance to stop listening to all events.
Example
See the example below to understand how to use the Reactter.offAll
method:
8 collapsed lines
1import 'package:reactter/reactter.dart';2
3enum CustomEvent { myEvent }4
5class MyController {6 final stateA = Signal(0);7 final stateB = Signal('InitialValue');8}
51 collapsed lines
1import 'package:reactter/reactter.dart';2import './my_controller.dart';3
4void main() {5 // Listen to the `myEvent` event of the `MyController` instance before it's created6 Reactter.on(7 ReactterDependency<MyController>,8 CustomEvent.myEvent,9 (instance, param) => print('Event emitted with param: $param'),10 );11
12 // Create a new instance of `MyController`13 final myController = Reactter.create(() => MyController())!;14
15 // Listen to the `didUpdate` event of the `MyController` instance16 Reactter.on<MyController, ReactterState>(17 myController,18 Lifecycle.didUpdate,19 (instance, state) => print('State: ${state.runtimeType}'),20 );21
22 // Listen to the `didUpdate` event of the `stateA`23 Reactter.on(24 myController.stateA,25 Lifecycle.didUpdate,26 (MyController instance, Signal state) => print('State A updated: ${state.value}'),27 );28
29 // Listen to the `didUpdate` event of the `stateB`30 Reactter.on(31 myController.stateB,32 Lifecycle.didUpdate,33 (MyController instance, Signal state) => print('State B updated: ${state.value}'),34 );35
36 // Change the value of `stateA` to `10`37 // Print:38 // State: Signal<int>39 // State A updated: 1040 myController.stateA.value = 10;41
42 // Change the value of `stateB` to `'Hello World!'`43 // Print:44 // State: Signal<String>45 // State B updated: 'Hello World!'46 myController.stateB.value = 'Hello World!';47
48 // Emit the `myEvent` event with the parameter `42`49 // Print: Event emitted with param: 4250 Reactter.emit(ReactterDependency<MyController>, CustomEvent.myEvent, 42);51
52 // Stop listening to all events of the `MyController` instance53 Reactter.offAll(myController);17 collapsed lines
54
55 // Change the value of `stateA` to `20`56 // The `didUpdate` event of `MyController` instance is not listened to57 // Print:58 // State A updated: 2059 myController.stateA.value = 20;60
61 // Change the value of `stateB` to `'Hey you!'`62 // The `didUpdate` event of `MyController` instance is not listened to63 // Print:64 // State B updated: 'Hey you!'65 myController.stateB.value = 'Hey you!';66
67 // Emit the `myEvent` event with the parameter `test`68 // The `myEvent` event of `MyController` instance is not listened to69 Reactter.emit(ReactterDependency<MyController>, CustomEvent.myEvent, 'test');70}