Native Shopify Mobile App with 20 new features
Kickstart your hyperlocal marketplace in Corona pandemic with a starter guide
Android App Development
iOS App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 188.8.131.52 release ready!
Owner and Founder. Opencart
We are going to learn about Consumer and Selector in Flutter. These terms come in the Flutter provider concept.
It is the most important topic if you care which widget will rebuild when anything updates on the widget : |
Let’s start with Consumer and selector what is it and why we use it? in Flutter provider.
The provider is using state management in Flutter, Consumer is playing a specific role. Whenever any value update in ChangeNotifier class or we can whenever notifyListeners() is called in ChangeNotifier class then we get the updated value from Consumer block on UI screen. We can call it a listener which listens to updated values
So we can update the UI based on latest data which changed/updated recently.
Lets see the code example –
Whenever CartModel update any value and call notifyListeners then the above block will be executed with cart model instance.
Consumer is Good in state management. Okay then why we use Selector ?
Wait !…….. The consumer has a drawback when you use multiple time consumers at a different-different place for different-different values, for example, the cart model has a total price, product qty, and the seller name.
When you update any of three value in the cart model then all three consumer block will be called and corresponding widget will rebuild.
Suppose you update product quantity then product quantity, total price, and seller name widget will rebuild again. Here, Only product quantity and total price should be rebuilt but the seller name also rebuilds.
To avoid this drawback selector will use in the Flutter provider. It’s working the same as a consumer expect one thing let us see by the code example.
Yes, It will call only when seller name will update because we listens only seller name changes.
Thanks for reading.
Reference – https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple
Checkout our blogs – https://mobikul.com/category/flutter/
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.