Member-only story
Decoupling logic from UI in Flutter application
Introduction
Recently I worked on a Flutter project. I started with the UI definition. Then I extended the application with user interaction handlers and business logic rules. In no time, the widget classes became polluted. The readability and reusability of the code were low. But the major problem was testing. I could not validate business rules without launching the whole application or initialising the widgets.
After a while, I decoupled UI from the application logic. I refactored the codebase to a set of widget classes, an application state class, and the business rules class.
Such architecture allowed me to test every component in isolation. And I didn’t need to use widgets to verify application logic. Additionally, using the provider package allowed me to inject the required application state into a widget instance and test UI behaviour in isolation. Before, I did a sequence of UI interactions to bring the application to the needed state.
In this article, I would like to share my experience and show how I detached UI from application logic. As an example, I am going to write a simple calculator application.