Managing app lifecycle state with Getx in flutter?
Asked Answered
N

2

5

what is the best approach to managing app lifecycle state?

would you do it using a getxservice with WidgetsBindingObserver?

thanks in advance.

Nigrescent answered 10/11, 2021 at 3:27 Comment(0)
S
13

You can extend your controller class with FullLifeCycleController instead of GetXController. FullLifeCycleController actually extends GetXController with WidgetsBindingObserver.

This gist might help you out. :)

Sharynshashlik answered 13/11, 2021 at 16:24 Comment(2)
YourController extends FullLifeCycleController with FullLifeCycleMixin{ // GetxController }Gildea
This solution is no longer useful. Try this instead: #69908154Gildea
G
5

Extend your controller with FullLifeCycleController with FullLifeCycleMixin.

Example Code:

class HomeController extends FullLifeCycleController with FullLifeCycleMixin {

  @override
  void onInit() {
    //
    super.onInit();
  }

  // Mandatory
  @override
  void onDetached() {
    print('HomeController - onDetached called');
  }

  // Mandatory
  @override
  void onInactive() {
    print('HomeController - onInative called');
  }

  // Mandatory
  @override
  void onPaused() {
    print('HomeController - onPaused called');
  }

  // Mandatory
  @override
  void onResumed() {
    print('HomeController - onResumed called');
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        log("App Resumed");
        instantSubmit();
        break;
      case AppLifecycleState.inactive:
        log("App InActive");
        break;
      case AppLifecycleState.paused:
        log("App Paused");
        break;
      case AppLifecycleState.detached:
        log("App Detached");
        break;
      case AppLifecycleState.hidden:
        // TODO: Handle this case.
        log("Hidden");
        break;
    }
  }

}

Gildea answered 5/1 at 15:31 Comment(1)
why the @override arn't working ?Psychopharmacology

© 2022 - 2024 — McMap. All rights reserved.