Dart DocumentationobserveObservableBox<T>

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

new ObservableBox([T initialValue]) #

ObservableBox([T initialValue]) : _value = initialValue;

Properties

final Stream<List<ChangeRecord>> changes #

inherited from ObservableBase

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.

docs inherited from Observable
Stream<List<ChangeRecord>> get changes {
 if (_broadcastController == null) {
   _broadcastController =
       new StreamController<List<ChangeRecord>>.broadcast(sync: true);
 }
 return _broadcastController.stream;
}

final bool hasObservers #

inherited from ObservableBase

True if this object has any observers, and should call notifyPropertyChange for changes.

bool get hasObservers => _broadcastController != null &&
                        _broadcastController.hasListener;

T value #

T get value => _value;
void set value(T newValue) {
 _value = notifyPropertyChange(const Symbol('value'), _value, newValue);
}

Methods

void notifyChange(ChangeRecord record) #

inherited from ObservableBase

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) #

inherited from ObservableBase

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.

docs inherited from Object
String toString() => '#<$runtimeType value: $value>';