0

When a view is inheriting GetView , it's basically a StatelessWidget with additional variable named "controller" which is our entrypoint to access anything inside the GetXController. The problem is, I do not have any control over the view lifecycle like I do in Kotlin android Such as onCreate, onStart, onResume,onPause, onDestroy,etc. when inheriting the GetView which is a StatelessWidget, Can I use a stateful widget together with GetXController?, Will that be a time bom in my application that one day it will cause a problem or performance issue?

2 Answers2

1

GetxController have all lifecycle function you want, just create one and put it in. Cause getx split your view and logic code, all lifecycle function will put in controller.

As you can see GetxController was have GetLifeCycleMixin in there code.

Tuan
  • 2,033
  • 1
  • 9
  • 16
  • I want to access the lifecycle method in View layer, I mean like seperating the controller lifecycle and the view lifecycle. – Louis Aldorio May 11 '22 at 10:29
  • `GetView` was design to use with `GetxController` man, if you don't want use GetxController, use `StateFulWidget` insteads. – Tuan May 11 '22 at 10:34
  • What about using GetXController together with StatefulWidget ?, Will that be a problem ? – Louis Aldorio May 12 '22 at 01:55
  • @LouisAldorio no, just make sure you are binding/put controller before use it, you can get instance of controller by `Get.find()` – Tuan May 12 '22 at 02:47
1

Try using this also you dont need statefull for the lifecycle on that stateless is enough with getview

so try call SuperController or FullLifeCycleController

import 'package:get/get.dart';

class ControllerLifeCycle1 extends SuperController{
  @override
  void onDetached() {
    // TODO: implement onDetached
  }
  @override
  void onInactive() {
    // TODO: implement onInactive
  }
  @override
  void onPaused() {
    // TODO: implement onPaused
  }

  @override
  void onResumed() {
    // TODO: implement onResumed
  }
}

class ControllerLifeCycle2 extends FullLifeCycleController  with FullLifeCycleMixin{
  @override
  void onDetached() {
    // TODO: implement onDetached
  }

  @override
  void onInactive() {
    // TODO: implement onInactive
  }
  @override
  void onPaused() {
    // TODO: implement onPaused
  }
  @override
  void onResumed() {
    // TODO: implement onResumed
  }

}

Just try this both on which one would work for you also they do have onDelete onClose onReady

as for example although it might outdated but just try to make reference above i put.

// I Also looking for lifecycle and stumble this one as per example e.g. gist.github.com/eduardoflorence/d918d05ad71175b52c2aca95588c305d

Arbiter Chil
  • 1,061
  • 1
  • 6
  • 9
  • I want to handle the lifecycle in view layer not in the controller layer, for example, the use case of it is plug in a listener, as we know listener in view is one time init, so do we have any solution to have the seperate controller lifecycle and view lifecycle ? – Louis Aldorio May 11 '22 at 10:03
  • Have you tried create binding/s on it? create a controller which handle lifecycle and put to view layer so you have both controller now which handle the lifecycle and the view controller layer. – Arbiter Chil May 11 '22 at 10:13
  • So do mean that, I control the view lifecycle from the GetXController?, instead of inside the view ?, as far as I know, it is not a good practice, because it reminds me of android design pattern which was called MVP (model, View , Presenter) which is tighly coupled, I am trying to make it like MVVM, so that controller does not control the view, and view explicitly, intead view got to observe the changes of the data then act, not act base on the instruction of controller. – Louis Aldorio May 11 '22 at 10:23