我阅读了文档,但不清楚。
它声明“[initState is] 当这个对象被插入到树中时调用。”
当一个小部件被插入到树中时,就意味着它已经被创建,这意味着类构造函数被调用。初始化的目的是什么?构造函数的目的不就是初始化类实例吗?
谢谢你们的时间。
最佳答案
不同之处在于(在创建 State
对象的上下文中)它具有 initState()
方法:
constructor
simply create a newState
instance
initState()
is called after the object is created and at this point you have access to theBuildContext
or theStatefulWidget
to which theState
is attached to, respectively using thecontext
and thewidget
properties. At this point theState
is already mounted.
引用状态
:https://api.flutter.dev/flutter/widgets/State-class.html
引用挂载状态
:https://api.flutter.dev/flutter/widgets/State/mounted.html
关于android - Flutter 中的 initState 和类构造函数有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52066976/