dispatchStateIfNeed method
延迟分发:如果 观察者 在 widget 初始化时注册,那么两者生命周期一直保持同步;如果 观察者 在 widget 生周期中后期才注册,则会丢失已经过去的前期生命周期回调,需要补上 延迟/补充的生命周期分发 如果用户 #addObserver 时机较晚,会导致前期对应的生命周期丢失,这里延迟分发
Implementation
void dispatchStateIfNeed(LifecycleState state) {
int stateIndex = state.index;
if (stateIndex >= LifecycleState.onInit.index) {
dispatchState(LifecycleState.onInit);
}
if (stateIndex >= LifecycleState.onCreate.index) {
dispatchState(LifecycleState.onCreate);
}
if (stateIndex >= LifecycleState.onStart.index) {
dispatchState(LifecycleState.onStart);
}
if (stateIndex >= LifecycleState.onResume.index) {
dispatchState(LifecycleState.onResume);
}
/// 以下条件几乎不会触发
if (stateIndex >= LifecycleState.onPause.index) {
dispatchState(LifecycleState.onPause);
}
if (stateIndex >= LifecycleState.onStop.index) {
dispatchState(LifecycleState.onStop);
}
if (stateIndex >= LifecycleState.onDestroy.index) {
dispatchState(LifecycleState.onDestroy);
}
}