DataSubject<T> class

A DataSubject is a Subject whose notifier is a DataNode. It provides access to the DataNode data through a getter. A DataSubject observer can therefore receive data through a notification.

Example:

// A reference to an int
class IntReference {
  int value;
  IntReference(this.value);
}

/// Increments a [IntReference] by the `step` value.
class IncrementIntReference extends DataNode<IntReference> {
  final int step;
  IncrementIntReference(this.step, IntReference data) : super(data);

  @override
  Status update() {
    print('increment by $step');
    data.value += step;
    return Status.success;
  }
}

void dataSubjectObserverExample() {
  // subject data is an int reference
  final subjectData = IntReference(0);

  // subject notifier simply increments its int reference
  // and returns Status.success
  final incrementer = IncrementIntReference(2, subjectData);

  // the notifier will succeed on every attempt because it
  // always returns Status.success
  final dataSubject = DataSubject(incrementer);

  // the observer knows it will subscribe to a data subject;
  // its handler will therefore use a DataSubject parameter
  final observer = Observer(handler: (DataSubject<IntReference> subject) {
    print('received notification with data: ${subject.data.value}');
  });

  dataSubject.subscribe(observer);

  // update the data subject, the observer receives notification
  dataSubject.update();
}
Inheritance

Constructors

DataSubject(DataNode<T> notifier)
Constructs a DataSubject instance.

Properties

data → T
Access the data contained by the notifier DataNode.
no setter
hashCode int
The hash code for this object.
no setterinherited
notifications Status
finalinherited
notifier Node
getter/setter pairinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notify() → void
Notifies each _ObserverBase by calling their respective receive(subject) method and passing this as the _SubjectBase of the notification.
inherited
reset() → void
Resets the notification Node.
inherited
subscribe(_ObserverBase observer) → void
Add an _ObserverBase to the notification list.
inherited
toString() String
A string representation of this object.
inherited
unsubscribe(_ObserverBase observer) → void
Remove an _ObserverBase from the notification list.
inherited
update() Status
Updates the notification Node and if the Status it returns is part of the Status notification list, the Subject notifies all subscribed _ObserverBase instances.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited