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
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 Reactter . on
method:
import 'package:reactter/reactter.dart' ;
enum CustomEvent { myEvent }
final stateA = Signal ( 0 ) ;
final stateB = Signal ( 'InitialValue' ) ;
import 'package:reactter/reactter.dart' ;
import './my_controller.dart' ;
// Listen to the `myEvent` event of the `MyController` instance before it's created
ReactterDependency < MyController > ,
( MyController instance , int param) {
print ( 'Event emitted with param: $ param ' ) ;
// Create a new instance of `MyController`
final myController = Reactter . create (() => MyController ()) ! ;
// Listen to the `didUpdate` event of the `MyController` instance
Reactter . on < MyController , ReactterState > (
(instance , state) => print ( 'State: ${ state . runtimeType } ' ) ,
// Listen to the `didUpdate` event of the `stateA`
( MyController instance , Signal state) => print ( 'State A updated: ${ state . value } ' ) ,
// Listen to the `didUpdate` event of the `stateB`
( MyController instance , Signal state) => print ( 'State B updated: ${ state . value } ' ) ,
// Change the value of `stateA` to `10`
myController . stateA . value = 10 ;
// Change the value of `stateB` to `'Hello World!'`
// State B updated: 'Hello World!'
myController . stateB . value = 'Hello World!' ;
// Emit the `myEvent` event with the parameter `42`
// Print: Event emitted with param: 42
Reactter . emit ( ReactterDependency < MyController > , CustomEvent . myEvent , 42 ) ;
// Delete the `MyController`
Reactter . delete < MyController >() ;
// Can't change the value of `stateA`
// Error: "Can't update when it's been disposed"
// myController.stateA.value = 20;
// The `myEvent` event is not emitted,
// because the `MyController` together to the listeners were deleted.
Reactter . emit ( ReactterDependency < MyController > , CustomEvent . myEvent , "test" ) ;
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
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 Reactter . one
method:
import 'package:reactter/reactter.dart' ;
import './my_controller.dart' ;
// Listen to the `myEvent` event of the `MyController` instance before it's created
ReactterDependency < MyController > ,
( MyController instance , int param) {
print ( 'Event emitted with param: $ param ' ) ;
// Create a new instance of `MyController`
final myController = Reactter . create (() => MyController ()) ! ;
// Listen to the `didUpdate` event of the `MyController` instance
Reactter . one < MyController , ReactterState >(
(instance , state) => print ( 'State: ${ state . runtimeType } ' ) ,
// Listen to the `didUpdate` event of the `stateA`
( MyController instance , Signal state) => print ( 'State A updated: ${ state . value } ' ) ,
// Listen to the `didUpdate` event of the `stateB`
( MyController instance , Signal state) => print ( 'State B updated: ${ state . value } ' ) ,
// Change the value of `stateA` to `10`
myController . stateA . value = 10 ;
// Change the value of `stateB` to `'Hello World!'`
// State B updated: 'Hello World!'
myController . stateB . value = 'Hello World!' ;
// Emit the `myEvent` event with the parameter `42`
// Print: Event emitted with param: 42
Reactter . emit ( ReactterDependency < MyController > , CustomEvent . myEvent , 42 ) ;
// The `myEvent` event is not listened to again
Reactter . emit ( ReactterDependency < MyController > , CustomEvent . myEvent , 'test' ) ;
// Delete the `MyController`
Reactter . delete < MyController >() ;
// Can't change the value of `stateA`
// Error: "Can't update when it's been disposed"
// myController.stateA.value = 20;
// The `myEvent` event is not listened to again
Reactter . emit ( ReactterDependency < MyController > , CustomEvent . myEvent , "test" ) ;
Reactter . emit
The Reactter . 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 Reactter . emit
method:
import 'package:reactter/reactter.dart' ;
enum CustomEvent { myEvent }
final stateA = Signal ( 0 ) ;
final stateB = Signal ( 'InitialValue' ) ;
import 'package:reactter/reactter.dart' ;
import './my_controller.dart' ;
// Listen to the `myEvent` event of the `MyController` instance before it's created
ReactterDependency < MyController > ,
( MyController instance , int param) {
print ( 'Event emitted with param: $ param ' ) ;
// Create a new instance of `MyController`
final myController = Reactter . create (() => MyController ()) ! ;
// Listen to the `didUpdate` event of the `MyController` instance
Reactter . on < MyController , ReactterState > (
(instance , state) => print ( 'State: ${ state . runtimeType } ' ) ,
// Listen to the `didUpdate` event of the `stateA`
( MyController instance , Signal state) => print ( 'State A updated: ${ state . value } ' ) ,
// Listen to the `didUpdate` event of the `stateB`
( MyController instance , Signal state) => print ( 'State B updated: ${ state . value } ' ) ,
// Change the value of `stateA` to `10`
myController . stateA . value = 10 ;
// Change the value of `stateB` to `'Hello World!'`
// State B updated: 'Hello World!'
myController . stateB . value = 'Hello World!' ;
// Emit the `myEvent` event with the parameter `42`
// Print: Event emitted with param: 42
Reactter . emit ( ReactterDependency < MyController > , CustomEvent . myEvent , 42 ) ;
// Delete the `MyController`
Reactter . delete < MyController >() ;
// Can't change the value of `stateA`
// Error: "Can't update when it's been disposed"
// myController.stateA.value = 20;
// The `myEvent` event is not emitted,
// because the `MyController` together to the listeners were deleted.
Reactter . emit ( ReactterDependency < MyController > , CustomEvent . myEvent , "test" ) ;
Reactter . off
The Reactter . 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 Reactter . off
method:
import 'package:reactter/reactter.dart' ;
enum CustomEvent { myEvent }
final stateA = Signal ( 0 ) ;
final stateB = Signal ( 'InitialValue' ) ;
import 'package:reactter/reactter.dart' ;
import './my_controller.dart' ;
void onMyEvent (instance , param) {
print ( 'Event emitted with param: $ param ' ) ;
void onDidUpdate (instance , state) {
print ( 'State: ${ state . runtimeType } ' ) ;
void onDidUpdateStateA ( MyController instance , Signal state) {
print ( 'State A updated: ${ state . value } ' ) ;
void onDidUpdateStateB ( MyController instance , Signal state) {
print ( 'State B updated: ${ state . value } ' ) ;
// Listen to the `myEvent` event of the `MyController` instance before it's created
ReactterDependency < MyController > ,
// Create a new instance of `MyController`
final myController = Reactter . create (() => MyController ()) ! ;
// Listen to the `didUpdate` event of the `MyController` instance
Reactter . on < MyController , ReactterState > (
// Listen to the `didUpdate` event of the `stateA`
// Listen to the `didUpdate` event of the `stateB`
// Change the value of `stateA` to `10`
myController . stateA . value = 10 ;
// Change the value of `stateB` to `'Hello World!'`
// State B updated: 'Hello World!'
myController . stateB . value = 'Hello World!' ;
// Emit the `myEvent` event with the parameter `42`
// Print: Event emitted with param: 42
Reactter . emit ( ReactterDependency < MyController > , CustomEvent . myEvent , 42 ) ;
// Stop listening to the `didUpdate` event of the `stateA`
// Change the value of `stateA` to `20`
// Print: State: Signal <int>
myController . stateA . value = 20 ;
// Stop listening to the `didUpdate` event of the `MyController` instance
// Change the value of `stateB` to `'Hey you!'`
// Print: State B updated: 'Hey you!'
myController . stateB . value = 'Hey you!' ;
// Stop listening to the `myEvent` event of the `MyController` instance
ReactterDependency < MyController > ,
// Emit the `myEvent` event with the parameter `test`
// The `myEvent` event is not listened to again
Reactter . emit ( ReactterDependency < MyController > , CustomEvent . myEvent , 'test' ) ;
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
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 Reactter . offAll
method:
import 'package:reactter/reactter.dart' ;
enum CustomEvent { myEvent }
final stateA = Signal ( 0 ) ;
final stateB = Signal ( 'InitialValue' ) ;
import 'package:reactter/reactter.dart' ;
import './my_controller.dart' ;
// Listen to the `myEvent` event of the `MyController` instance before it's created
ReactterDependency < MyController > ,
(instance , param) => print ( 'Event emitted with param: $ param ' ) ,
// Create a new instance of `MyController`
final myController = Reactter . create (() => MyController ()) ! ;
// Listen to the `didUpdate` event of the `MyController` instance
Reactter . on < MyController , ReactterState > (
(instance , state) => print ( 'State: ${ state . runtimeType } ' ) ,
// Listen to the `didUpdate` event of the `stateA`
( MyController instance , Signal state) => print ( 'State A updated: ${ state . value } ' ) ,
// Listen to the `didUpdate` event of the `stateB`
( MyController instance , Signal state) => print ( 'State B updated: ${ state . value } ' ) ,
// Change the value of `stateA` to `10`
myController . stateA . value = 10 ;
// Change the value of `stateB` to `'Hello World!'`
// State B updated: 'Hello World!'
myController . stateB . value = 'Hello World!' ;
// Emit the `myEvent` event with the parameter `42`
// Print: Event emitted with param: 42
Reactter . emit ( ReactterDependency < MyController > , CustomEvent . myEvent , 42 ) ;
// Stop listening to all events of the `MyController` instance
Reactter . offAll (myController) ;
// Change the value of `stateA` to `20`
// The `didUpdate` event of `MyController` instance is not listened to
myController . stateA . value = 20 ;
// Change the value of `stateB` to `'Hey you!'`
// The `didUpdate` event of `MyController` instance is not listened to
// State B updated: 'Hey you!'
myController . stateB . value = 'Hey you!' ;
// Emit the `myEvent` event with the parameter `test`
// The `myEvent` event of `MyController` instance is not listened to
Reactter . emit ( ReactterDependency < MyController > , CustomEvent . myEvent , 'test' ) ;