rememberValueNotifier<T> method
ValueNotifier<T>
rememberValueNotifier<T>({})
快速生成一个可用的类型 ValueNotifier
- 确定是否需要更新对象只有 type 和 key
value
,factory
,factory2
确定如何初始化的创建一个 ValueNotifier 必须有一个不能为空 不作为更新keylisten
当前的 Context 自动监听生成的 ValueNotifier 不作为更新key 只有首次有效 后续变化无效
Implementation
ValueNotifier<T> rememberValueNotifier<T>({
T? value,
T Function()? factory,
T Function(Lifecycle)? factory2,
FutureOr<void> Function(ValueNotifier<T>)? onDispose,
bool listen = false,
Object? key,
}) =>
remember<ValueNotifier<T>>(
factory2: (l) {
assert(value != null || factory != null || factory2 != null,
'value and factory and factory2 cannot be null at the same time');
value ??= factory?.call();
value ??= factory2?.call(l);
final r =
CancellableValueNotifier(value as T, l.makeLiveCancellable());
if (listen && this is Element) {
final rContext = WeakReference(this);
r.addCListener(l.makeLiveCancellable(), () {
final element = rContext.target as Element?;
if (element != null) {
element.markNeedsBuild();
}
});
}
return r;
},
onDispose: (d) {
d.dispose();
onDispose?.call(d);
},
key: FlexibleKey(value, factory, factory2, key),
);