FutureSubject<T> class
A FutureSubject takes a Function returning a Future of argument type
T
. The FutureSubject will call the argument future and use the result to
build a DataNode using the argument builder
Function. The built
DataNode is then used as the notifier
of the FutureSubject.
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 futureSubjectExample() {
// keep a scopy reference for testing purposes
final intReference = IntReference(0);
final futureSubject = FutureSubject<IntReference>(
future: () async {
// simulate some future based operation
print('calling future');
await Future<void>.delayed(const Duration(seconds: 2));
// return a value from the future
return intReference;
},
// builds a DataNode using the result of the future
builder: (data) {
return IncrementIntReference(5, data);
},
);
final observer = Observer(handler: (subject) {
print('notified by future subject: ${intReference.value}');
});
futureSubject.subscribe(observer);
// calls the future function and notifies the
// observer using the built DataNode when the
// future completes
futureSubject.update();
}
Constructors
-
FutureSubject({required Future<
T> future(), required DataNode<T> builder(T), List<Status> notifications = const [Status.success]}) - Constructs a FutureSubject instance.
Properties
- 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 respectivereceive(subject)
method and passingthis
as the_SubjectBase
of the notification.inherited -
rebuild(
) → void -
Schedules the future to be called and the
notifier
DataNode to be rebuilt using the result of the future. -
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 -
Calls the future and once a result is available the contained data is
updated and the FutureSubject is updated as if it were a
DataSubject.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited