Home > Mobile >  Flutter getx: separate each binding inside own binding when I don't use navigator
Flutter getx: separate each binding inside own binding when I don't use navigator

Time:11-08

I am using getx. I have a main Page inside my application and mainBinding. Inside main page I have a Tabbar. Main Page has many pages that any pages loaded in this way:

child: TabBarView(
  controller: controller.tabController,
  physics: const NeverScrollableScrollPhysics(),
  children: [
    ReportOverallPage(),
    Container(),
    Container(),
    Container(),
    Container(),
    Container(),....
  ],
),

As you can see ReportOverallPage() page not used of Navigator means not pushed into stack bay navigator so I cant create a binding for each pages.

each pages have own repository and controller. Now I have to add all controllers and repositories inside MainBinding.

class MainViewBinding implements Bindings {
  @override
  void dependencies() {
    Get.lazyPut<MainViewController>(() => MainViewController());

    Get.lazyPut<ReportController>(
        () => ReportController(Get.find<ReportRepository>()));
    Get.lazyPut<ReportRepository>(
        () => ReportRepositoryImp(Get.find<RestClient>()));
    .........
    
  }
}

Now MainBainding has a long list of Putted class.How can I separate each binding class inside own binding class when I don't use navigator?

CodePudding user response:

I'm afraid there's no straightforward way to do that. Because bindings are page specific (or navigation) you have to bind the dependencies with the page. Tabs aren't pages. Therefore you don't directly bind dependencies to them. You need to bind them with the containing class.

But one workaround could be you create separate binding class and then call the dependencies() method of each of them inside the dependencies() method of your MainBinding class.

  • Related