简体   繁体   English

在 Flutter Documentation, Explanation of Provider.of 段

[英]In Flutter Documentation, Explanation of Provider.of paragraph

This Given Link Describes Provider.of 这个给定的链接描述了 Provider.of

in the text its using a example of a basic shopping app with items whic can be added by tapping.在文本中,它使用了一个基本购物应用程序的示例,其中包含可以通过点击添加的项目。 Here Provider.of is used to remove all contents of the cart.这里 Provider.of 用于删除购物车的所有内容。 and it mentions " We could use Consumer for this, but that would be wasteful. We'd be asking the framework to rebuild a widget that doesn't need to be rebuilt."它提到“我们可以为此使用消费者,但这会很浪费。我们会要求框架重建一个不需要重建的小部件。”

Provider.of<CartModel>(context, listen: false).removeAll();

Here we can clearly see contents of the cart are changed and hence requires a rebuild... then why does it uses PRovider.of and mentions it doesnt require a rebuild.在这里我们可以清楚地看到购物车的内容已更改,因此需要重建...那么为什么它使用 PROvider.of 并提到它不需要重建。

use case of this snipped code is very clear.这段代码的用例非常清楚。

Provider.of<CartModel>(context, listen: false).removeAll();

use it whenever you don't need to change your UI.只要您不需要更改 UI,就可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM