ObservableBox<T> class
An observable box that holds a value. Use this if you want to store a single value. For other cases, it is better to use ObservableList, ObservableMap, or a custom Observable implementation based on ObservableMixin. The property name for changes is "value".
class ObservableBox<T> extends ObservableBase { T _value; ObservableBox([T initialValue]) : _value = initialValue; T get value => _value; void set value(T newValue) { _value = notifyPropertyChange(const Symbol('value'), _value, newValue); } String toString() => '#<$runtimeType value: $value>'; }
Extends
ObservableBase > ObservableBox<T>
Constructors
Properties
final Stream<List<ChangeRecord>> changes #
The stream of change records to this object.
Changes should be delivered in asynchronous batches by calling queueChangeRecords.
deliverChangeRecords can be called to force delivery.
Stream<List<ChangeRecord>> get changes { if (_broadcastController == null) { _broadcastController = new StreamController<List<ChangeRecord>>.broadcast(sync: true); } return _broadcastController.stream; }
final bool hasObservers #
True if this object has any observers, and should call notifyPropertyChange for changes.
bool get hasObservers => _broadcastController != null && _broadcastController.hasListener;
Methods
void notifyChange(ChangeRecord record) #
Notify observers of a change. For most objects notifyPropertyChange is more convenient, but collections sometimes deliver other types of changes such as a ListChangeRecord.
void notifyChange(ChangeRecord record) { if (!hasObservers) return; if (_changes == null) { _changes = []; queueChangeRecords(_deliverChanges); } _changes.add(record); }
dynamic notifyPropertyChange(Symbol field, Object oldValue, Object newValue) #
Notify that the field name
of this object has been changed.
The oldValue and newValue are also recorded. If the two values are identical, no change will be recorded.
For convenience this returns newValue. This makes it easy to use in a setter:
var _myField;
get myField => _myField;
set myField(value) {
_myField = notifyPropertyChange(
const Symbol('myField'), _myField, value);
}
notifyPropertyChange(Symbol field, Object oldValue, Object newValue) { if (hasObservers && !identical(oldValue, newValue)) { notifyChange(new PropertyChangeRecord(field)); } return newValue; }
String toString() #
Returns a string representation of this object.
String toString() => '#<$runtimeType value: $value>';