Introduction
One should separate it's UI from the Business Logic.
- The business logic layer should be in charge of deciding how to react to the user’s actions and how to delegate tasks like retrieving and saving data to other classes.
- Communicating between these layers is important as well.
- The easy way is to just create those classes when you need them.
- What if two classes each have their own database handler class and make conflicting calls to the database? Both Android and iOS use Dependency Injection or DI to create instances in one place and inject them into other classes that need them.
In Flutter, data moves from top to bottom.
- If you have data in a child widget that you want to send up to a parent, you should use one of the global state management methods or move the state up(lifting the state up).
State is just data that changes over the lifecycle of the app.
- When stateful data changes, the UI reacts by painting the widgets to reflect the new state.
- UI is just a visual representation of a given state.
f(state) => UI
.
- There can be Local State or Global or Shared App State.
Two state types to consider are ephemeral state, also known as UI state and app state.
- Use Ephemeral state when no other component in the widget tree needs to access a widget’s data.
- Examples include whether a TabBarView tab is selected or FloatingActionButton is pressed
- Use App state when other parts of your app need to access a widget’s state data.
- One example is an image that changes over time, like an icon for the current weather.
- Another example is information that the user selects on one screen and which should then display on another screen, like when the user adds an item to a shopping cart.
As the app grows in complexity, one is likely to encounter bugs directly related to the way data flows through your app via user input.
Managing the state changes carefully helps you avoid soul-crushing bugs that only happen at runtime and can also help optimize performance.
Many solutions to state management exist some are built into flutter and others are available as an external library these includes: