前言
上一篇我们讲到网络请求等异步操作最好放在 GetxController
的 onReady
生命周期函数中处理。本篇我们来介绍 GetxController
的生命周期函数。
GetxController 类
我们自定义的Controller
继承关系如下所示。
其中 GetxController
只是有个 update
方法用于通知组件刷新。在 DisposableInterface
中覆盖了onInit 方法,实际多干了一件事:
SchedulerBinding.instance?.addPostFrameCallback((_) => onReady());
其实就是将 onReady
方法作为回调,当 onInit
完成之后的一帧来调用 onInit
。这也就是我们上一篇说的,onReady
会在 onInit
完成后一帧后调用。这里有三个方法:
onInit
:组件在内存分配后会被马上调用,可以在这个方法对 controller 做一些初始化工作。onReady
:上一篇我们介绍过,这里是在 onInit 一帧