How to have separate controller for each screen in flutter with clean architecture

803 Views Asked by At

I'm trying to implement Clean Architecture in my flutter app. There is a module named Purchase Order in my app. This contains List Screen, Filter Screen, Add Purchase Order Screen.

Currently, I have one controller (GetxController) for all of them. All of the business logic for all of those screens is included in that controller. The reason for not having separate controllers is the dependency on these screens on each other.

Let's say the user applies filters from Filter Screen, which should update the List Screen. Likewise, the user can remove filters and change some filter values from the List Screen.

Likewise, if the user adds a new Purchase Order, that should update the List Screen with the newly added Purchase Order

How to achieve the communication between multiple controllers in accordance with the clean architecture.

3

There are 3 best solutions below

0
Rami Al Debs On

Hope you are fine I believe the issue here is a state management issue, not a clean architecture issue.

I would approach it in 2 different ways:

1-Same controller with copyWith state: you can have one controller and 3 listeners on each screen the state should hold the 3 screens state -> also it should implement copyWith function copyWith creates a new instance of the state with previous data in case you want to modify a parameter and keep the other ones therefore whenever you want to update a screen, all screens will be updated.

2- Controllers and listners the second one is having a separate controller for each screen, and a listener on the constructor of each one, whenever a controller has changes the others will be notified.

0
Nikita Shadkov On

There is a great guy answered this question. https://stackoverflow.com/a/67224552/10766511

You can use Get.find<FirstController>().valueFromFirstController to access different controller values

So, in clean architecture you will be able to store it in different packages and fiels. I would suggest you to use dependency injection also, to achieve this, you can use get_it package

0
Faiz Ahmad Dae On

I use get_cli to manage and organize my projects And I suggest this to you because it is a very regular structure and good management for projects

get_cli give you the best structure for view, controller, model, and providers

Installation:

  1. flutter pub global activate get_cli
  2. in your [project folder]: get init