美文网首页
Flutter StatefulWidget 生命周期

Flutter StatefulWidget 生命周期

作者: wheelsMaker | 来源:发表于2022-12-27 13:42 被阅读0次

    一般提到 flutter 的生命周期,有 App 的生命周期和StatefulWidget 生命周期两个概念。本文只讨论 StatefulWidget 的生命周期。

    StatefulWidget 生命周期指的是 StatefulWidget 从创建到销毁的过程,实际上体现在 State 类的的方法的调用顺序和调用时机。

    1.createState

    createStateStatefulWidget 创建 State 的方法。从 State 的角度来说,这个方法是个构造方法,只会调用一次。
    但是从 StatefulWidget 角度来说,这个方法会在 StatefulWidget 的生命周期里调用多次,如果 StatefulWidget 在树的不同位置,framework 会调用 createState 多次来创建不同的 State。如果StatefulWidget从树中移除再插入树的话,framework 会调用 createState 来刷新 State。

    调用时机:当 StatefulWidget 插入树中调用。

      /// 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 之后调用,只会调用一次,一般只在这里执行只需要调用一次的初始化方法,例如订阅者。

    ///  * 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 之后调用,如果调用了 [BuildContext.dependOnInheritedWidgetOfExactType]方法,InheritedWidget 在数据改变时也会调用 didChangeDependencies 方法。

    4.build

    build() 主要是用于构建 widget 子树的,会在如下场景被调用:

    在调用initState()之后。
    在调用didUpdateWidget()之后。
    在调用setState()之后。
    在调用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 需要更新的时候会调用次方法,将 widget 属性和新的 widget 绑定,在这里对 widget 属性的更新做处理,此方法执行后会调用build方法,所以无需调用 setState。

    6.reassemble

    reassemble 只在热重载(hot reload)时会被调用,此回调在Release模式下永远不会被调用。可以调试的时候对数据进行重新初始化的操作。

    7.deactivate

    当 State 从树中移除的时候调用。

    8.activate

    activate 是在 deactivate 之后重新插入树时调用。一般发生在使用 GlobalKey 从树的某一个位置移动到另一个位置。在调用完activate后会自动调 build 方法重建。

    9.dispose

    dispose当 State 永久的从树中移除的时候会调用。mounted 会变为false,无法调用setState,在此方法需要释放掉持有的资源。

    StatefulWidget 生命周期.png
    参考资料

    《Flutter实战·第二版》

    相关文章

      网友评论

          本文标题:Flutter StatefulWidget 生命周期

          本文链接:https://www.haomeiwen.com/subject/urrnqdtx.html