Signal class
Signals help you to establish connections between two entities.
It is essentially a communication mechanism. When an entity wants to notify others about something, it creates an signal and emit it. It doesn't care about who wants to listen to the signal. The interested parties will connect to the signal instead. They will disconnect from the signal when there's no more need to listen to it.
Let us create a class that will emit signals for others to connect to.
class SignalEmitter {
final somethingHappened = Signal();
}
Let's create another class that is interested in listening to the signal we just created.
class SignalReceiver {
Slot handleSomethingHappened() {
print("something happened.");
}
!Note: Slot is just a typedef for void. It is used to highlight that the method is intended to be used as a slot.
Connect the slot to the signal to get notified about signal emission.
signalEmitter.somethingHappened.connect(signalReceiver.handleSomethingHappened);
//connect(signalEmitter.somethingHappened, signalReceiver.handleSomethingHappened); <- or like this
Now we will emit the signal to notify others.
signalEmitter.somethingHappened();
Running the code would print "something happened."
Disconnect the slot from the signal when it is no more needed.
signalEmitter.somethingHappened.disconnect(signalReceiver.handleSomethingHappened);
//disconnect(signalEmitter.somethingHappened, signalReceiver.handleSomethingHappened); <- or like this
The Signal class offering all the signal features.
Constructors
- Signal()
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call<
T> ([T? argument]) → void - Calls the slots connected with this signal. Optionally can pass an argument to the slot as well. If an argument is provided but the slot doesn't take one, then the argument is discarded and the slot is still called.
-
connect(
Function slot) → void - in built connect function for object flavoured usage.
-
disconnect(
Function slot) → void - in built disconnect function for object flavoured usage.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited