[英]How does Provider.of<>(context) listen to updates to an object?
Theoretically, if the class of the object is a ChangeNotifier/Listenable, I can see how that would be possible.从理论上讲,如果 object 的 class 是 ChangeNotifier/Listenable,我可以看到这怎么可能。 But if the class is a plain Dart class, how does Provider.of<>(context) listen to updates and rebuilds the widget when the object changes?但是,如果 class 是一个普通的 Dart class,当 object 发生变化时,Provider.of<>(context) 如何监听更新并重建小部件?
Just to clarify, when you use ChangeNotifier/Listenable and update your values, your values actually updates, now the problem is how do you let your widget know there's an updated value for rebuild, that's what provider tries to solve by letting your use Provider.of(context, listen:true)
to listen for changes in values and when this happens your initial value or widget holding that value is destroyed and a new one is created for you with the updated values, likewise ConsumerWidget helps you achieve the same thing by rebuilding the children holding values with the updated value.澄清一下,当您使用 ChangeNotifier/Listenable 并更新您的值时,您的值实际上会更新,现在的问题是您如何让您的小部件知道有一个更新的重建值,这就是提供者试图通过让您使用Provider.of(context, listen:true)
来监听值的变化,当这种情况发生时,您的初始值或持有该值的小部件将被销毁,并使用更新后的值为您创建一个新的,同样,ConsumerWidget 可以帮助您实现相同的目的用更新后的值重建持有值的孩子。
PS: you have to inform your providers or consumers about the updated values with notifylisteners()
for them to rebuild when there's a change in values. PS:您必须使用notifylisteners()
通知您的提供者或消费者有关更新值的信息,以便他们在值发生变化时重建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.