Obs<T> class

响应式变量对象,在 ObsBuilder 中使用会自动建立依赖,当变量更新时会自动重建 UI:

const count = Obs(0);

ObsBuilder(
  builder: (context) => Text('${count.value}'),
),
Inheritance
Implementers

Constructors

Obs(T _value, {bool autoNotify = true, bool? immediate, ValueChanged<T>? onChanged, String? cacheKey, ElSerialize? serialize, Duration? expire, Duration? keepAliveTime, ElStorage? storage})
创建一个响应式变量,Obs 可以在任何地方创建,在 Widget 内部创建就是局部状态,在 Widget 外部创建就是全局状态, 设置全局状态

Properties

autoNotify bool
变量发生更新时自动通知 UI 更新,若你想手动执行 notify 通知 UI 更新,可以将其设置为 false。 此属性允许在任意地方动态修改它。
getter/setter pair
cacheKey String?
本地缓存 key,如果不为 null,每次更新变量时会将值缓存到本地,请保证 key 唯一
final
expire Duration?
设置本地持久化的过期时间
final
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
immediate bool?
是否立即执行一次 onChanged 监听函数
final
keepAliveTime Duration?
getter/setter pair
obsBuilders Set<VoidCallback>
记录已绑定的 ObsBuilder 小部件的刷新方法
final
onChanged ValueChanged<T>?
创建响应式变量时立即绑定监听函数
final
rawValue ↔ T
直接访问原始 _value 避免触发副作用函数
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
serialize → ElSerialize?
对象序列化接口,本地储存只允许基本数据类型,当响应式变量类型为对象时,你需要提供一个转换器,否则持久化时会抛出异常, 不过,如果对象实现了 ElSerializeElSerializeModel 接口,那么你无需指定此参数。
final
storage ElStorage
如果自定义本地存储为 null,那么将使用响应式变量默认的本地存储对象
no setter
value ↔ T
访问响应式变量,它会与 ObsBuilder 自动进行绑定
getter/setter pairoverride

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
bindBuilders() → void
将响应式变量与 ObsBuilder 进行绑定
dispose() → void
销毁响应式变量,如果 Obs 为局部状态(在 State 中创建),请在符合以下条件销毁它们:
override
execAutoNotify() → void
执行通知副作用函数前判断是否启用了 autoNotify
initLocalValue() → T?
获取本地缓存值,这是一个默认实现,对于 ListMap 集合,其相应的 Obs 会覆写此方法
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notify() → void
执行所有副作用监听函数、包括 UI 页面刷新
notifyChanged() → void
执行 onChanged 监听函数
notifyListeners() → void
Call all the registered listeners.
inherited
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
toString() String
当直接当作字符串插入时,无需指定 .value
override

Operators

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

Static Properties

expireLocalStorage ElStorage
存储 Obs 响应式变量的 Storage 对象(指定了 expire 过期时间的响应式变量)
no setter
localStorage ElStorage
存储 Obs 响应式变量的 Storage 对象
no setter

Static Methods

initStorage() Future<void>
初始化 Obs 响应式变量的本地存储对象