扑扑的生命周期 [英] Life cycle in flutter
问题描述
颤动是否具有类似于Activity.resume()
的方法,该方法可以告诉开发人员用户已经回到活动中.
Does flutter have a method like Activity.resume()
which can tell developer the user has gone back to the activity.
当我从Page-B中的Internet上选择数据并返回到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.
推荐答案
-
createState()
: 当框架被指示构建StatefulWidget时,它立即调用createState()
createState()
: When the Framework is instructed to build a StatefulWidget, it immediately callscreateState()
mounted
是真的:
当createState
创建状态类时,会将buildContext
分配给该状态.过度简化了buildContext
,此小部件在小部件树中的放置位置.这是更长的解释.
所有小部件都具有bool this.mounted
属性.分配buildContext
时,它变为true.卸载小部件时调用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()
:
移除State对象时,将调用dispose()
,这是永久的.您应该在此方法退订并取消所有动画,流等.
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 an error is thrown is setState
is called.
这篇关于扑扑的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!