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
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:
main . dart
my_dependency.dart
import 'package:flutter/material.dart' ;
import 'package:reactter/reactter.dart' ;
import 'my_dependency.dart' ;
// Listen to the `myEvent` event of the `MyDependency` before it's created.
RtDependency < MyDependency >() ,
(instance , param) => print ( 'CustomEvent emitted with param: $ param ' ) ,
// Create a new instance of `MyDependency`.
final myDependency = Rt . create (() => MyDependency ()) ! ;
// Listen to the `didUpdate` event of the `MyDependency` instance.
Rt . on < MyDependency , RtState > (
(instance , state) => print ( 'The state updated is: ${ state . runtimeType } ' ) ,
// Listen to the `didUpdate` event of the `stateA`.
(instance , Signal state) => print ( 'stateA updated: ${ state . value } ' ) ,
// Listen to the `didUpdate` event of the `stateB`.
(instance , Signal state) => print ( 'stateB updated: ${ state . value } ' ) ,
// Change the value of `stateA` to `10`.
// The state updated is: Signal <int>
myDependency . stateA . value = 10 ;
// Change the value of `stateB` to `'Hello World!'`.
// stateB updated: 'Hello World!'
// The state updated is: Signal <String>
myDependency . stateB . value = 'Hello World!' ;
// Emit the `myEvent` event with the parameter `test`.
// CustomEvent.myEvent emitted with param: test
Rt . emit (myDependency , CustomEvent . myEvent , 'test' ) ;
// Delete the `MyDependency` instance
Rt . delete < MyDependency >() ;
// Cannot listen to the `didUpdate` event of the `MyDependency` instance
// because it's deleted. This will throw an error.
// myDependency.stateA.value = 20;
// Cannot listen to the `myEvent` event using the `MyDependency` instance
Rt . emit (myDependency , CustomEvent . myEvent , 'This is not printed' ) ;
// Can still emit to the `myEvent` event using the ` RtDependency `
// CustomEvent.myEvent emitted with param: 42
Rt . emit ( RtDependency < MyDependency >() , CustomEvent . myEvent , 42 ) ;
class MyApp extends StatelessWidget {
Widget build ( BuildContext context) {
child : Text ( 'See the output in the terminal' ) ,
import 'package:reactter/reactter.dart' ;
enum CustomEvent { myEvent }
final stateA = Signal ( 0 ) ;
final stateB = Signal ( 'InitialValue' ) ;
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
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:
main . dart
my_dependency.dart
import 'package:flutter/material.dart' ;
import 'package:reactter/reactter.dart' ;
import 'my_dependency.dart' ;
// Listen to the `myEvent` event of the `MyDependency` before it's created.
RtDependency < MyDependency >() ,
(instance , param) => print ( 'CustomEvent emitted with param: $ param ' ) ,
// Create a new instance of `MyDependency`.
final myDependency = Rt . create (() => MyDependency ()) ! ;
// Listen to the `didUpdate` event of the `MyDependency` instance.
Rt . one < MyDependency , RtState >(
(instance , state) => print ( 'The state updated is: ${ state . runtimeType } ' ) ,
// Listen to the `didUpdate` event of the `stateA`.
(instance , Signal state) => print ( 'stateA updated: ${ state . value } ' ) ,
// Listen to the `didUpdate` event of the `stateB`.
(instance , Signal state) => print ( 'stateB updated: ${ state . value } ' ) ,
// Change the value of `stateA` to `10`.
// The state updated is: Signal <int>
myDependency . stateA . value = 10 ;
// Change the value of `stateB` to `'Hello World!'`.
// No print for the `didUpdate` event of `MyDependency` instance
// because it's a one-time listener.
// stateB updated: 'Hello World!'
myDependency . stateB . value = 'Hello World!' ;
// Emit the `myEvent` event with the parameter `test`.
// CustomEvent.myEvent emitted with param: test
Rt . emit (myDependency , CustomEvent . myEvent , 'test' ) ;
// Delete the `MyDependency` instance
Rt . delete < MyDependency >() ;
// Cannot listen to the `didUpdate` event of the `MyDependency` instance
// because it's deleted. This will throw an error.
// myDependency.stateA.value = 20;
/// Cannot listen to the `myEvent` event using the `MyDependency` instance
/// because the listener is one-time.
Rt . emit ( RtDependency < MyDependency >() , CustomEvent . myEvent , 42 ) ;
class MyApp extends StatelessWidget {
Widget build ( BuildContext context) {
child : Text ( 'See the output in the terminal' ) ,
import 'package:reactter/reactter.dart' ;
enum CustomEvent { myEvent }
final stateA = Signal ( 0 ) ;
final stateB = Signal ( 'InitialValue' ) ;
Rt . emit
The Rt . emit
method triggers an event from an instance, notifying all listeners of that event.
Syntax
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:
main . dart
my_dependency.dart
import 'package:flutter/material.dart' ;
import 'package:reactter/reactter.dart' ;
import 'my_dependency.dart' ;
// Listen to the `myEvent` event of the `MyDependency` before it's created.
RtDependency < MyDependency >() ,
(instance , param) => print ( 'CustomEvent emitted with param: $ param ' ) ,
// Create a new instance of `MyDependency`.
final myDependency = Rt . create (() => MyDependency ()) ! ;
// Listen to the `didUpdate` event of the `MyDependency` instance.
Rt . on < MyDependency , RtState > (
(instance , state) => print ( 'The state updated is: ${ state . runtimeType } ' ) ,
// Listen to the `didUpdate` event of the `stateA`.
(instance , Signal state) => print ( 'stateA updated: ${ state . value } ' ) ,
// Listen to the `didUpdate` event of the `stateB`.
(instance , Signal state) => print ( 'stateB updated: ${ state . value } ' ) ,
// Change the value of `stateA` to `10`.
// The state updated is: Signal <int>
myDependency . stateA . value = 10 ;
// Change the value of `stateB` to `'Hello World!'`.
// stateB updated: 'Hello World!'
// The state updated is: Signal <String>
myDependency . stateB . value = 'Hello World!' ;
// Emit the `myEvent` event with the parameter `test`.
// CustomEvent.myEvent emitted with param: test
Rt . emit (myDependency , CustomEvent . myEvent , 'test' ) ;
// Delete the `MyDependency` instance
Rt . delete < MyDependency >() ;
// Cannot listen to the `didUpdate` event of the `MyDependency` instance
// because it's deleted. This will throw an error.
// myDependency.stateA.value = 20;
// Cannot listen to the `myEvent` event using the `MyDependency` instance
Rt . emit (myDependency , CustomEvent . myEvent , 'This is not printed' ) ;
// Can still emit to the `myEvent` event using the ` RtDependency `
// CustomEvent.myEvent emitted with param: 42
Rt . emit ( RtDependency < MyDependency >() , CustomEvent . myEvent , 42 ) ;
class MyApp extends StatelessWidget {
Widget build ( BuildContext context) {
child : Text ( 'See the output in the terminal' ) ,
import 'package:reactter/reactter.dart' ;
enum CustomEvent { myEvent }
final stateA = Signal ( 0 ) ;
final stateB = Signal ( 'InitialValue' ) ;
Rt . off
The Rt . off
method removes a specific event listener from an instance, stopping it from receiving further notifications for that event.
Syntax
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:
main . dart
my_dependency.dart
import 'package:flutter/material.dart' ;
import 'package:reactter/reactter.dart' ;
import 'my_dependency.dart' ;
void onMyEvent (instance , param) {
print ( 'CustomEvent emitted with param: $ param ' ) ;
void onDidUpdate (instance , state) {
print ( 'The state updated is: ${ state . runtimeType } ' ) ;
void onDidUpdateStateA (instance , Signal state) {
print ( 'stateA updated: ${ state . value } ' ) ;
void onDidUpdateStateB (instance , Signal state) {
print ( 'stateB updated: ${ state . value } ' ) ;
// Listen to the `myEvent` event of the `MyDependency` before it's created.
RtDependency < MyDependency >() ,
// Create a new instance of `MyDependency`.
final myDependency = Rt . create (() => MyDependency ()) ! ;
// Listen to the `didUpdate` event of the `MyDependency` instance.
Rt . on < MyDependency , RtState > (
// Listen to the `didUpdate` event of the `stateA`.
Rt . on (myDependency . stateA , Lifecycle . didUpdate , onDidUpdateStateA) ;
// Listen to the `didUpdate` event of the `stateB`.
Rt . on (myDependency . stateB , Lifecycle . didUpdate , onDidUpdateStateB) ;
// Change the value of `stateA` to `10`.
// The state updated is: Signal <int>
myDependency . stateA . value = 10 ;
// Change the value of `stateB` to `'Hello World!'`.
// stateB updated: 'Hello World!'
// The state updated is: Signal <String>
myDependency . stateB . value = 'Hello World!' ;
// Emit the `myEvent` event with the parameter `test`.
// CustomEvent.myEvent emitted with param: test
Rt . emit (myDependency , CustomEvent . myEvent , 'test' ) ;
// Stop listening to the `myEvent` event of the `MyDependency`
RtDependency < MyDependency >() ,
// Stop listening to the `didUpdate` event of the `MyDependency` instance
Rt . off (myDependency , Lifecycle . didUpdate , onDidUpdate) ;
// Stop listening to the `didUpdate` event of the `stateA`
Rt . off (myDependency . stateA , Lifecycle . didUpdate , onDidUpdateStateA) ;
// No print for neither `stateA` nor the `MyDependency` instance
// because the listeners are removed
myDependency . stateA . value = 20 ;
// Cannot listen to the `myEvent` event using the `MyDependency` instance
// because the listener is removed
Rt . emit (myDependency , CustomEvent . myEvent , 'This is not printed' ) ;
class MyApp extends StatelessWidget {
Widget build ( BuildContext context) {
child : Text ( 'See the output in the terminal' ) ,
import 'package:reactter/reactter.dart' ;
enum CustomEvent { myEvent }
final stateA = Signal ( 0 ) ;
final stateB = Signal ( 'InitialValue' ) ;
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:
main . dart
my_dependency.dart
import 'package:flutter/material.dart' ;
import 'package:reactter/reactter.dart' ;
import 'my_dependency.dart' ;
// Listen to the `myEvent` event of the `MyDependency` before it's created.
RtDependency < MyDependency >() ,
(instance , param) => print ( 'CustomEvent emitted with param: $ param ' ) ,
// Create a new instance of `MyDependency`.
final myDependency = Rt . create (() => MyDependency ()) ! ;
// Listen to the `didUpdate` event of the `MyDependency` instance.
Rt . on < MyDependency , RtState > (
(instance , state) => print ( 'The state updated is: ${ state . runtimeType } ' ) ,
// Listen to the `didUpdate` event of the `stateA`.
(instance , Signal state) => print ( 'stateA updated: ${ state . value } ' ) ,
// Listen to the `didUpdate` event of the `stateB`.
(instance , Signal state) => print ( 'stateB updated: ${ state . value } ' ) ,
// Change the value of `stateA` to `10`.
// The state updated is: Signal <int>
myDependency . stateA . value = 10 ;
// Change the value of `stateB` to `'Hello World!'`.
// stateB updated: 'Hello World!'
// The state updated is: Signal <String>
myDependency . stateB . value = 'Hello World!' ;
// Emit the `myEvent` event with the parameter `test`.
// CustomEvent.myEvent emitted with param: test
Rt . emit (myDependency , CustomEvent . myEvent , 'test' ) ;
// Remove all event listeners of `stateA`.
Rt . offAll (myDependency . stateA) ;
// Remove all event listeners of `myDependency`,
// including the generic listeners (using ` RtDependency `).
Rt . offAll (myDependency , true ) ;
// No print for neither `stateA` nor the `MyDependency` instance
// because the listeners are removed
myDependency . stateA . value = 20 ;
// Change the value of `stateB` to `Hey you!'`.
// Only print for the `didUpdate` event of `stateB`.
// stateB updated: 'Hey you!'
myDependency . stateB . value = 'Hey you!' ;
// Cannot listen to the `myEvent` event using the `MyDependency` instance
// because the listeners are removed.
Rt . emit (myDependency , CustomEvent . myEvent , 'This is not printed' ) ;
class MyApp extends StatelessWidget {
Widget build ( BuildContext context) {
child : Text ( 'See the output in the terminal' ) ,
import 'package:reactter/reactter.dart' ;
enum CustomEvent { myEvent }
final stateA = Signal ( 0 ) ;
final stateB = Signal ( 'InitialValue' ) ;