简体   繁体   English

Provider.of<>(context) 如何监听 object 的更新?

[英]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.

相关问题 提供者.of<appbarcubit> (上下文,听:假)</appbarcubit> - Provider.of<AppBarCubit>(context, listen: false) Provider.of(context, listen: false) 是否等同于 context.read()? - Is Provider.of(context, listen: false) equivalent to context.read()? 如何调用ThemeService themeService = Provider.of(context); 在脚手架的初始状态? - How to call ThemeService themeService = Provider.of(context); in the initial state of a scaffold? 如何对访问 Provider.of(context) 的小部件进行 Flutter 测试 - How to do a Flutter Test for a widget that accesses Provider.of(context) 如何将 get_it 用于 Provider.of<exampleprovider> (语境)?</exampleprovider> - How to use get_it for Provider.of<ExampleProvider>(context)? 为什么我不能在 build() 中使用 context.read,但我可以使用 Provider.of with listen: false? - Why can't I use context.read in build(), but I can use Provider.of with listen: false? Flutter Provider:为什么调用Provider.of时会出现这个错误<widget> (上下文)像 tihis:</widget> - Flutter Provider: Why does this error happens when calling Provider.of<Widget>(context) like tihis: 为什么没有 `listen: false` Provider.of&lt;&gt; 不起作用? - Why Provider.of<> doesn't work without `listen: false`? InitState中的提供程序引发错误。 Provider.of <SomeProvider> (上下文) - Provider Throw Errors in InitState. Provider.of<SomeProvider>(context) 如何通过Provider.of访问数据 - How to access data through Provider.of
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM