![]() ![]() ![]() In this diagram you can see how not just one, but multiple widgets can subscribe to the model class for all of them to receive state changes. In addition, with this methodology we could also achieve the following: By doing so we are creating a circular reactive architecture.Ī great advantage of this model is that we have a clear separation between the state logic and the display of views, thus achieving a much cleaner code that is easier to organize and easier to maintain. On the left side we have a model class that will contain the variables that will constitute the state of our widget, this widget will subscribe to this model to receive state changes, and when those changes are made the model will emit the updated values to the widget. The change that we are going to apply consists of refactoring this state management so that it looks like this: This management is done through variables in the body of the class, when we want to change the state we use the setState() method. In this diagram we see a widget that is responsible for the visual part and state management. The default state management that Flutter gives you when you create a project is as follows: Provider architectureīefore we start I'm going to show you the change we're going to make. The resulting application is going to be a counter app in which we can increase the counter if we press the button located at the bottom of the screen:ĭoes this example sound familiar to you? Yes, what we are going to do is refactor the app template that Flutter creates when you create a project, leaving the functionality and the visual part intact, but refactoring its state management with provider.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |