Observable<T> class

请叫我code哥

//不支持缓存方式
class NoCacheCounterState {
  int counter = 0;
}
//使用
var observableCounter = Observable(NoCacheCounterState());

//支持缓存方式
class CacheCounterState {
  int counter;

  CacheCounterState({this.counter = 0});

  static CacheCounterState fromJson(dynamic json) {
    return CacheCounterState(
      counter: json["counter"],
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['counter'] = this.counter;
    return data;
  }
}

//使用
var observableCounter = Observable(CacheCounterState(),
    cacheKey: 'CounterState_CacheKey', decoder: CacheCounterState.fromJson);

Constructors

Observable(T state, {String? cacheKey, _Decoder? decoder, _PreContent? preWrite, _PreContent? preRead})
  • 设置cacheKey 即为生效,状态变化即会缓存,数据对象需要实现fromJson和toJson
  • 如果存在缓存,使用缓存赋值state
  • 写入前处理数据回调,读取前处理数据回调,一般用于加解密
  • cacheKey is set to take effect, state changes are cached, and data objects need to implement fromJson and toJson
  • if a cache exists, use the cache assignment state
  • write pre-processing data callback, read pre-processing data callback, generally used for encryption and decryption
  • Properties

    hashCode int
    The hash code for this object.
    no setterinherited
    runtimeType Type
    A representation of the runtime type of the object.
    no setterinherited
    state ↔ T
    getter/setter pair

    Methods

    listen(_Listen<T>? listen, {bool change = true}) → void
  • 监听变化 change = false, 不变化也会监听notify,不会触发builder
  • Listen for change = false, it listens for notify without changing, builder will not be triggered
  • noSuchMethod(Invocation invocation) → dynamic
    Invoked when a nonexistent method or property is accessed.
    inherited
    notify(_Callback<T> callback) → void
  • 通知状态改变,触发builder
  • Notifies a state change, triggering the Builder
  • toString() String
    A string representation of this object.
    inherited

    Operators

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