Skip to main content

Flutter provider ChangeNotifier, Flutter state management




Hi, thank you for joining me.  I name is  Sergio, and today we will introduce you to state management in Flutter. Use a small code base to create design patterns for scalable applications. I use the official example in the Flutter documentation.

Now that you understand declarative UI programming and the difference between temporary state and application state, you are ready to learn simple application state management.

The provider package is easy to understand and does not use much code. It also uses concepts that apply to all other methods.

To illustrate, consider the following simple application. The application has two independent screens: catalog and shopping cart. It may be a job posting application, but you can imagine the same structure in a simple social network application or directory.

The catalog screen includes a custom application bar and a scroll view of many list items. This is an application visualized as a tree of widgets.






So we have at least 5 subclasses of Widget. Many of them need to enter a state of "belonging" to other places.


This leads to our first question: where should we put the current state of the shopping cart?


By calling methods on it, it is difficult to force changes to the widget from the outside. Even if you can make this work successful, you will fight the framework instead of letting it help you.


Shopping cart Widget.updateWith(item);


You need to consider the current state of the UI and apply new data to it. It is difficult to avoid mistakes.


In Flutter, every time its content changes, you build a new widget. Whenever it changes, it will rebuild MyCart from above.





When we say that widgets are immutable, this is what we mean. They will not change-they will be replaced.




ChangeNotifierProvider


ChangeNotifierProvider is a widget that provides ChangeNotifier instances to its descendants.




Consumer

Now that CartModel is provided to the widgets in our application via the ChangeNotifierProvider declaration at the top, we can start using it.



The only parameter required by the Consumer widget is the builder. Builder is a function, it will be called whenever ChangeNotifier changes.


The best practice is to place your Consumer widget as deep as possible in the tree. You don’t want to rebuild most of the UI just because some details have changed somewhere.





Provider.of

We will ask the framework to rebuild a widget that does not need to be rebuilt.

For this use case, we can use Provider.of and set the listen parameter to false.





Very nice example for different architecture samples.



Links


https://github.com/brianegan/flutter_architecture_samples


https://github.com/flutter/samples/tree/master/provider_shopper


https://github.com/gskinnerTeam/flutter-folio


https://fluttersamples.com/



Comments

Popular posts from this blog

Pgpool PgBouncer Postgresql streaming replication, load balancing and administration

The term scalability refers to the ability of a software system to grow as the business that uses it grows. PostgreSQL provides some features to help you build scalable solutions, but strictly speaking, PostgreSQL itself is not scalable. It can effectively use the following resources from one computer. Now, we will show you some configurations that may be useful for your use case. However, this can be problematic when distributing the database solution to multiple computers, because the standard PostgreSQL server can only run on a single computer. In this article, we will study different extension schemes and their implementation in PostgreSQL. Replication can be used in many expansion scenarios. Its main purpose is to create and maintain a backup database when the system fails. This is especially true for physical replication. However, replication can also be used to improve the performance of PostgreSQL-based solutions. Sometimes third-party tools can be used to implement complex exp

Tekstverwerking python Text processing python SpaCy, TensorFlow, NLTK, Allen-NLP, Stanford-NLP

 Dit post maakt gebruik van spaCy, een populaire Python-bibliotheek die de taalgegevens en algoritmen bevat die je nodig hebt om teksten in natuurlijke taal te verwerken. Zoals u in dit post zult leren, is spaCy gemakkelijk te gebruiken omdat het containerobjecten biedt die elementen van natuurlijke taalteksten vertegenwoordigen, zoals zinnen en woorden. Deze objecten hebben op hun beurt attributen die taalkenmerken vertegenwoordigen, zoals delen van spraak. Op het moment van schrijven bood spaCy voorgetrainde modellen aan voor Engels, Duits, Grieks, Spaans, Frans, Italiaans, Litouws, Noors Bokmål, Nederlands, Portugees en meerdere talen gecombineerd. Bovendien biedt spaCy ingebouwde visualizers die u programmatisch kunt aanroepen om een grafische weergave van de syntactische structuur van een zin of benoemde entiteiten in een document te genereren.   De spaCy-bibliotheek ondersteunt ook native geavanceerde NLP-functies die andere populaire NLP-bibliotheken voor Python niet hebben. Spa

Google Closure

   Closure Library De Closure-bibliotheek is een JavaScript-bibliotheek, vergelijkbaar met andere moderne producten zoals jQuery, Angular, Vue.js, Dojo en MooTools. De coderingsstijl en het gebruik van opmerkingen in de Closure-bibliotheek zijn op maat gemaakt voor Closure Compiler. In vergelijking met andere JavaScript-bibliotheken is het de belangrijkste onderscheidende factor van Closure Compiler. Een eenvoudig compressie-experiment ontdekte dat wanneer Closure Compiler wordt gebruikt in plaats van YUI Compressor, de Closure Lib-code met 85% kan worden verminderd, wat een enorme impact kan hebben op de codecompressiecapaciteit van de compiler.    De implementatie van de  closure bibliotheek richt zich ook op leesbaarheid en prestaties. Wees zuinig bij het maken van objecten, maar wees genereus bij het benoemen en opnemen van objecten. Het heeft ook een prachtig gebeurtenissysteem, ondersteuning voor klassen en overerving en verschillende UI-componenten, waaronder bijvoorbeeld een ri