Flutter StatefulWidget 生命周期

一般提到 flutter 的生命周期,有 App 的生命周期和StatefulWidget 生命周期兩個概念。本文只討論 StatefulWidget 的生命周期。

StatefulWidget 生命周期指的是 StatefulWidget 從創(chuàng)建到銷毀的過程,實際上體現(xiàn)在 State 類的的方法的調(diào)用順序和調(diào)用時機。

1.createState

createStateStatefulWidget 創(chuàng)建 State 的方法。從 State 的角度來說,這個方法是個構(gòu)造方法,只會調(diào)用一次。
但是從 StatefulWidget 角度來說,這個方法會在 StatefulWidget 的生命周期里調(diào)用多次,如果 StatefulWidget 在樹的不同位置,framework 會調(diào)用 createState 多次來創(chuàng)建不同的 State。如果StatefulWidget從樹中移除再插入樹的話,framework 會調(diào)用 createState 來刷新 State。

調(diào)用時機:當 StatefulWidget 插入樹中調(diào)用。

  /// The framework can call this method multiple times over the lifetime of
  /// a [StatefulWidget]. For example, if the widget is inserted into the tree
  /// in multiple locations, the framework will create a separate [State] object
  /// for each location. Similarly, if the widget is removed from the tree and
  /// later inserted into the tree again, the framework will call [createState]
  /// again to create a fresh [State] object, simplifying the lifecycle of
  /// [State] objects.

2.initState

initState 是在 createState 之后調(diào)用,只會調(diào)用一次,一般只在這里執(zhí)行只需要調(diào)用一次的初始化方法,例如訂閱者。

///  * In [initState], subscribe to the object.
  ///  * In [didUpdateWidget] unsubscribe from the old object and subscribe
  ///    to the new one if the updated widget configuration requires
  ///    replacing the object.
  ///  * In [dispose], unsubscribe from the object.

3. didChangeDependencies

didChangeDependencies 在 initState 之后調(diào)用,如果調(diào)用了 [BuildContext.dependOnInheritedWidgetOfExactType]方法,InheritedWidget 在數(shù)據(jù)改變時也會調(diào)用 didChangeDependencies 方法。

4.build

build() 主要是用于構(gòu)建 widget 子樹的,會在如下場景被調(diào)用:

在調(diào)用initState()之后。
在調(diào)用didUpdateWidget()之后。
在調(diào)用setState()之后。
在調(diào)用didChangeDependencies()之后。
在State對象從樹中一個位置移除后,又重新插入到樹的其他位置之后。

  /// The framework calls this method in a number of different situations. For
  /// example:
  ///
  ///  * After calling [initState].
  ///  * After calling [didUpdateWidget].
  ///  * After receiving a call to [setState].
  ///  * After a dependency of this [State] object changes (e.g., an
  ///    [InheritedWidget] referenced by the previous [build] changes).
  ///  * After calling [deactivate] and then reinserting the [State] object into
  ///    the tree at another location.

5.didUpdateWidget

當 widget 需要更新的時候會調(diào)用次方法,將 widget 屬性和新的 widget 綁定,在這里對 widget 屬性的更新做處理,此方法執(zhí)行后會調(diào)用build方法,所以無需調(diào)用 setState。

6.reassemble

reassemble 只在熱重載(hot reload)時會被調(diào)用,此回調(diào)在Release模式下永遠不會被調(diào)用??梢哉{(diào)試的時候?qū)?shù)據(jù)進行重新初始化的操作。

7.deactivate

當 State 從樹中移除的時候調(diào)用。

8.activate

activate 是在 deactivate 之后重新插入樹時調(diào)用。一般發(fā)生在使用 GlobalKey 從樹的某一個位置移動到另一個位置。在調(diào)用完activate后會自動調(diào) build 方法重建。

9.dispose

dispose當 State 永久的從樹中移除的時候會調(diào)用。mounted 會變?yōu)閒alse,無法調(diào)用setState,在此方法需要釋放掉持有的資源。

StatefulWidget 生命周期.png
參考資料

《Flutter實戰(zhàn)·第二版》

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容