Karan V
Karan V

Reputation: 363

Managing app lifecycle state with Getx in flutter?

what is the best approach to managing app lifecycle state?

would you do it using a getxservice with WidgetsBindingObserver?

thanks in advance.

Upvotes: 6

Views: 12690

Answers (2)

Ujjawal Maurya
Ujjawal Maurya

Reputation: 616

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;
    }
  }

}

Upvotes: 6

Mayur Agarwal
Mayur Agarwal

Reputation: 1834

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

This gist might help you out. :)

Upvotes: 14

Related Questions