About Flutter MVVM Architecture with GetX

76 Views Asked by At

I have the following structure:

  • ReportPageview

  • ReportController

  • ReportService ....

My question is about the controller. My code was too long, cause I have a lot of differents items inside of it. So, my screen have the components separated already, but I was confusing how to do it with my controller.

What I Did:

I created anothers controllers to do the logic, example:

  • ReportAgeController (that controller will do all the logic for the age part, like count the items by age range)
  • ReportGenderController (that controller will do all the logic for the gender part, like count the items by gender)

So, inside my ReportController there is:

class ReportController extends GetxController { 
    late ReportService _reportService;

    late AgeController ageController; 
    late GenderController genderController;

    onInit(){ 
        _reportService = Get.find<ReportService>(); 
        ageController = ageController(); 
        genderController = genderController(reportService: _reportService); 
    }

    onReady(){ 
        _getItems(); 
        _generateReport();
    } 

    _getItems(){
        items = _reportservice.getItems();
    }

    List items = [];

    _generateReport() { 
        for (var item in items) {      
            ageController.setAgeRange(item);       
            genderController.setGenderRange(item);   
        } 
        update();
    }
    ... another logic here
} 

In this case, I can call the filtered items like

Text("Male count ${controller.genderController.all.toString()}");

My question is: AgeController and GenderController are not a service, but I have no idea if I can call them a Controller too.

How can a call these classes and how could be my structure?

My actual strucuture after my changes is:

  • ReportPageview.dart

  • ReportController.dart

  • ItemsController (folder)

    • AgeController.dart

    • GenderController.dart

  • ReportService.dart

0

There are 0 best solutions below