颤振中的生命周期

颤振中的生命周期

问题描述:

flutter 有没有像 Activity.resume() 这样的方法可以告诉开发者用户已经返回到 Activity.

Does flutter have a method like Activity.resume() which can tell developer the user has gone back to the activity.

当我在Page-B中从互联网上取数据并返回Page-A时,如何让Page-A知道数据已准备好.

When I pick the data from internet in Page-B and go back to Page-A, how can I let Page-A know that the data is prepared.

  1. createState():当框架被指示构建一个 StatefulWidget 时,它会立即调用 createState()

mounted 为真:createState 创建您的状态类时,会为该状态分配一个 buildContext.buildContext 过于简化,是小部件树中放置此小部件的位置.这是一个更长的解释.所有小部件都有一个 bool this.mounted 属性.当 buildContext 被分配时,它变成真.卸载小部件时调用 setState 是错误的.

mounted is true: When createState creates your state class, a buildContext is assigned to that state. buildContext is, overly simplified, the place in the widget tree in which this widget is placed. Here's a longer explanation. All widgets have a bool this.mounted property. It is turned true when the buildContext is assigned. It is an error to call setState when a widget is unmounted.

initState():这是在创建小部件时调用的第一个方法(当然在类构造函数之后.)initState 被调用一次且仅一次.它必须调用 super.initState().

initState(): This is the first method called when the widget is created (after the class constructor, of course.) initState is called once and only once. It must call super.initState().

didChangeDependencies():在第一次构建小部件时,会在 initState 之后立即调用此方法.

didChangeDependencies(): This method is called immediately after initState on the first time the widget is built.

build():这个方法经常被调用.它是必需的,它必须返回一个 Widget.

build(): This method is called often. It is required, and it must return a Widget.

didUpdateWidget(Widget oldWidget):如果父小部件更改并且必须重建此小部件(因为它需要为其提供不同的数据),但它正在使用相同的 runtimeType 进行重建,则调用此方法.这是因为 Flutter 正在重用长期存在的状态.在这种情况下,您可能希望再次初始化一些数据,就像在 initState 中一样.

didUpdateWidget(Widget oldWidget): If the parent widget changes and has to rebuild this widget (because it needs to give it different data), but it's being rebuilt with the same runtimeType, then this method is called. This is because Flutter is re-using the state, which is long lived. In this case, you may want to initialize some data again, as you would in initState.

setState():此方法通常由框架本身和开发人员调用.用于通知框架数据发生变化

setState(): This method is called often from the framework itself and from the developer. Its used to notify the framework that data has changed

deactivate():当 State 从树中移除时会调用 Deactivate,但它可能会在当前帧更改完成之前重新插入.之所以存在这种方法,主要是因为 State 对象可以从树中的一个点移动到另一个点.

deactivate(): Deactivate is called when State is removed from the tree, but it might be reinserted before the current frame change is finished. This method exists basically because State objects can be moved from one point in a tree to another.

dispose():dispose() 在 State 对象被移除时被调用,这是永久性的.此方法是您应该取消订阅和取消所有动画、流等的地方.

dispose(): dispose() is called when the State object is removed, which is permanent. This method is where you should unsubscribe and cancel all animations, streams, etc.

mounted 是假的:状态对象永远无法重新挂载,如果调用 setState 会抛出错误.

mounted is false: The state object can never remount, and error will be thrown if setState is called.