扑朔迷离,ValueNotifier,ChangeNotifier,Statenotifier有什么区别?
ValueNotifier,ChangEnotifier,Statenotifier有什么区别?
What is difference between valueNotifier,changeNotifier,stateNotifier?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ValueNotifier是扩展ChangeNotifier的一种特殊类型类,它可以容纳一个值并通知窗口小部件,每当其持有值变化时正在倾听它的小部件。
ChangeNotifier是向听众提供更改通知的类。这意味着您可以订阅与changeNotifier扩展或混合的类,并在该类更改时调用其notifyListeners()方法。此呼叫将通知此类订阅的小部件进行重建。
另一方面,Statenotifier是一种不可变的状态管理解决方案,可以在该解决方案中直接在通知者中更改状态。
ValueNotifier is a special type of class that extends Changenotifier, which can hold a single value and notifies the widgets which are listening to it whenever its holding value gets change.
ChangeNotifier is a class that provides change notification to its listeners. That means you can subscribe to a class that is extended or mixed in with ChangeNotifier and call its notifyListeners() method when there’s a change in that class. This call will notify the widgets that are subscribed to this class to rebuild.
On the other hand, StateNotifier is an immutable state management solution where the state can be directly changed within the notifier only.
ValueNotifier和Statenotifier之间存在有趣的区别。前者使用
==
来评估是否需要更新,而后来使用相同
检查。只要使用不变的类型,这就具有积极的绩效含义。参见 https://github.com/rrousselgit/state_notifier#why-are-listeners-called-when-the-new-new-state-is---------------------to-the-the-the-the-the-the-the-the-previous-state for notal in hold-in value类型”和枚举它们的工作相同。
一个人可能很想将valueNotifier用于可变对象,但这不错,因为可以通过突变方法更改该对象的子对象,这显然不会触发更新。
Statenotifier还具有一些其他选项,例如在实际更新该值时修改。
因此,我目前的建议是:
Statenotifier旨在与不变的对象一起使用,但是语言中没有任何机制来确保情况。此编译:
There is an interesting difference between ValueNotifier and StateNotifier. The former uses
==
to assess whether update is needed, while later usesidentical
check. This has positive performance implications in favor of the later as long as immutable types are used. See https://github.com/rrousselGit/state_notifier#why-are-listeners-called-when-the-new-state-is--to-the-previous-stateFor built-in "value types" and enums they work just the same.
One may be tempted to use ValueNotifier for mutable objects, but this doesn't work well because sub-objects of that object can be changed via mutating methods, and this clearly does not trigger updates.
StateNotifier also has some additional options, such as modifying when the value is actually updated.
Therefore my current recommendation is:
StateNotifier is intended to be used with immutable objects, but there is no mechanism in the language to ensure this is the case. This compiles: