What are some popular architectures that are suitable for Flutter App Development (and which one to choose)?
It boils down to what level of complexity you need to handle. 👇
Thread. 🧵
(1/2) Model–view–controller (MVC) is a software design pattern made of 3 types of components:
- Model: directly manages the data, logic, and rules of the application
- View: UI components (widgets)
- Controller: Accepts input and converts it to commands for the model or view
(2/2) MVC typically overloads the model with too much logic, and an additional service layer is often added: MVC+S
The Model should be reactive (e.g. using ChangeNotifier) so that the widgets rebuild when the model changes.
And user inputs always go through the controller.
(1/2) Model-view-viewmodel is also a design pattern made of 3 components:
- Model: directly manages the data, logic, and rules of the application
- View: UI components (widgets)
- ViewModel: mediates between the view and the model.
(2/2) The View and the ViewModel are connected with a data-binding based on the observer pattern.
When usign MVVM, the view can't access the model directly.
Rather, the ViewModel handles user inputs and converts the data from the Model so it can be easily presented.
Clean Architecture takes separation of concerns to the next level.
It is quite complex, requiring a bigger learning curve and considerably more boilerplate.
Some people swear by it and say it's worthwhile for large codebases:
(1/2) Stacked is a package + architecture that began as a MVVM implementation for Flutter.
- View: Shows the UI to the user.
- ViewModel: Manages the View state, business logic, and logic to handle user interaction
- Services: similar to the data layer in other architectures
(2/2) Stacked provides a base architecture and an extensive set of useful widgets that make your life easier so that you don't have to reinvent the wheel.
Here's how to use it to hide sensitive info when the app goes to the background. 🧵
How to set it up?
- add the WidgetsBindingObserver *mixin* to one of your classes (generally a State subclass)
- register the observer inside initState (and dispose it when done)
Then, just override the methods for the changes you want to keep track of.
In this case, we listen to app lifecycle changes and update a "foreground" flag via setState().