0

Maybe I am misunderstanding something here, but I have followed the docs on https://pub.dev/packages/get to set up a controller for each of my pages.

e.g standard page stuff:

Pages:

PageIntro -> PageLogin -> PageHome etc.  

Controllers:

- IntroController
- LoginController
- HomeController

I've defined each of my pages the same way, e.g.:

class PageIntro extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final IntroController c = Get.put(IntroController(context));

    return Scaffold(
      body: _buildContent(), // you get the idea.
    );
  }
}

I was expecting that when navigating away from PageIntro to PageLogin, that the IntroController would be deleted, but it's not.

I know I can manually delete controllers with:

Get.delete<IntroController>();

But I shouldn't have to.

Unless I'm missing something/doing something really stupid, this seems more hassle, more messy that just having PageIntro extend StatefulWidget, then doing whatever I need to do in the dispose override method as usual.

Please can someone more knowledgable on this inform me?

Thanks.

Dave
  • 5,283
  • 7
  • 44
  • 66
  • According to to the documentation, I think you have to use Bindings. https://github.com/jonataslaw/getx/blob/master/documentation/en_US/dependency_management.md#bindings – Adnan Alshami Sep 02 '21 at 20:27

2 Answers2

1

In your case, binding is the way to go.

On the other hand, controllers are removed when the route is removed from the stack. In your case, you maybe pushing to the next screen rather than replacing it.

Muhammad Talha
  • 165
  • 1
  • 8
1

I think it will help you to avoid this error. You should call in inside of build method.

  @override
  Widget build(BuildContext context) {
    var controller = ControllerStateDispatcher<RecentScreenController>(
        RecentScreenController(),true) // if you want to save state, false if you want reset state)
        .get<RecentScreenController>();
  }


    abstract class Cleaner {
      void clear();
    }
    
    abstract class GetControllerProvider<T extends GetxController> {
      R get<R extends GetxController>();
    }
    
    class ControllerStateDispatcher<T extends GetxController> extends Cleaner
        implements GetControllerProvider {
    
      bool shouldSaveState;
    
      T controller;
    
      ControllerStateDispatcher(this.controller, [this.shouldSaveState = false]);
    
      @override
      void clear() {
        Get.delete<T>();
      }
    
      @override
      DispatcherController get<DispatcherController extends GetxController>() {
        if (!shouldSaveState) clear();
    
        controller = Get.put(controller);
        return controller as DispatcherController;
      }
    }
xaldarof
  • 75
  • 1
  • 7