0
votes

This Given Link Describes 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. 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.

1
What is your question? - Christopher Moore
Think about a page, where you are not showing anything related to your cart, but you placed a button to clear the cart. If you are using Consumer and change the data the build method will be called, but you are not using anything in that page. So in this case you can use provider.of instead of consumer. - Midhun MP

1 Answers

0
votes

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.