Obs<T> constructor

Obs<T>(
  1. T _value, {
  2. bool autoNotify = true,
  3. bool? immediate,
  4. ValueChanged<T>? onChanged,
  5. String? cacheKey,
  6. ElSerialize? serialize,
  7. Duration? expire,
  8. Duration? keepAliveTime,
  9. ElStorage? storage,
})

创建一个响应式变量,Obs 可以在任何地方创建,在 Widget 内部创建就是局部状态,在 Widget 外部创建就是全局状态, 设置全局状态

Implementation

Obs(
  this._value, {
  this.autoNotify = true,
  this.immediate,
  this.onChanged,
  this.cacheKey,
  this.serialize,
  this.expire,
  Duration? keepAliveTime,
  ElStorage? storage, // 为当前响应式变量指定自定义存储对象
}) : super(_value) {
  _storage = storage;

  // 如果设置了缓存 key,则会将本地值覆盖初始值,并监听响应式变量的更改,实时同步至本地
  if (cacheKey != null) {
    _value = _getLocalValue();
    addListener(_setLocalValue);
  }

  if (immediate == true) safeCallback(notifyChanged);

  this.keepAliveTime = keepAliveTime;
}