preface

In the last article we talked about how asynchronous operations such as network requests are best handled in GetxController’s onReady lifecycle function. In this article we introduce the GetxController lifecycle function.

GetxController class

Our custom Controller inheritance is shown below.

GetxController simply has an update method that notifies the component of the refresh. Overriding the onInit method in DisposableInterface actually does one more thing:

SchedulerBinding.instance? .addPostFrameCallback((_) => onReady());Copy the code

It’s basically calling onInit one frame after onInit is done with the onReady method as a callback. So that’s what we said in the last post, onReady is going to be called one frame after onInit is done. Here are three methods:

  • onInitThe component is called as soon as memory is allocated. You can do some initialization on the Controller in this method.
  • onReadyThis is called after a frame of onInit and is suitable for navigational entry events such as dialog prompts, snackbars, or asynchronous network requests.
  • onCloseIn:onDeleteMethod, used for destructioncontrollerResources used, such as closing event listeners, closing stream objects, or destroying objects that might cause memory leaks, such asTextEditingController.AniamtionController. It is also useful for offline persistence of data.

Further up is the GetLifeCycle class, which simply configures the lifecycle in the constructor and is actually the $configureLifeCycle method that calls GetLifeCycleBase of mixin. Virtually all lifecycle methods are defined in this mixin. Specific methods are as follows:

  • onStartThe: component is called at the time of memory allocation, which is afinalMethod and use the internalcallableType to avoid being overridden by subclasses.
  • onDelete: Also afinalMethod, type andonStartSame, same cannot be covered. incontrollerDo not destroy before calling.

In fact, onStart and onDelete are bound to the internal _onStart and _onDelete methods, respectively, where the onInit and onClose methods are called.

/// Called in the constructor of GetLifeCycle
void $configureLifeCycle() {
  _checkIfAlreadyConfigured();
  onStart._callback = _onStart;
  onDelete._callback = _onDelete;
}

bool _initialized = false;

/// Checks whether the controller has already been initialized.
bool getinitialized => _initialized; ,void _onStart() {
    if (_initialized) return;
    onInit();
    _initialized = true;
  }

  bool _isClosed = false;

  /// Checks whether the controller has already been closed.
  bool get isClosed => _isClosed;

  // Internal callback that starts the cycle of this controller.
  void _onDelete() {
    if (_isClosed) return;
    _isClosed = true;
    onClose();
  }
Copy the code

OnStart is called at the controller memory allocation point, immediately after the memory allocation is complete

Alternative StatefulWidget

With the life cycle of GetxController, we can replace the StatefulWidget entirely.

  • OnInit or onReady replaces initState, as in the following code:
@override
void onInit() {
  // Network request or other initialization
  apiService.getData();
  super.onInit();
}
Copy the code
  • OnClose Replaces dispose, such as closing the stream:
class Controller extends GetxController {
  StreamController<String> name = StreamController<String> ();// ...

  @override
  void onClose() {
    name.close();
    super.onClose(); }}Copy the code

conclusion

Based on the above analysis, we get the life cycle of GetxController and its corresponding description as shown in the figure below.

There are only three ways we can access it throughout the lifecycle:

  • OnInit: initializes controllers, such as some member properties;
  • OnReady: ready service processing, such as asynchronous operation, navigation input parameter processing, etc.
  • OnClose: Releases resources to avoid memory leaks and to persist data.

By understanding the life cycle of GetxController, we can know what is appropriate to do in each life cycle.


I am dao Code Farmer with the same name as my wechat official account. This is a column about the introduction and practice of Flutter, providing systematic learning articles about Flutter. See the corresponding source code here: The source code of Flutter Introduction and Practical column. If you have any questions, please add me to the wechat account: island-coder.

👍🏻 : feel the harvest please point a praise to encourage!

🌟 : Collect articles, easy to look back!

💬 : Comment exchange, mutual progress!