android - Flutter 中的 initState 和类构造函数有什么区别?

标签 android ios flutter

我阅读了文档,但不清楚。

它声明“[initState is] 当这个对象被插入到树中时调用。”

当一个小部件被插入到树中时,就意味着它已经被创建,这意味着类构造函数被调用。初始化的目的是什么?构造函数的目的不就是初始化类实例吗?

谢谢你们的时间。

最佳答案

不同之处在于(在创建 State 对象的上下文中)它具有 initState() 方法:

  • constructor simply create a new State instance

  • initState() is called after the object is created and at this point you have access to the BuildContext or the StatefulWidget to which the State is attached to, respectively using the context and the widget properties. At this point the State 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/

相关文章:

android - 如何使用 Flutter 从网站上抓取图像?

android - 设置Textview + Edittext + Button

java - 在Android中通过httpost发送字节数组

ios - KVO 在 iOS 9.3 中损坏

java - Gradle 任务 assembleDebug 失败,退出代码为 1(运行时异常)

flutter - 如何具有动态容器大小

android - 下载 Android SDK 组件以进行离线安装

php - 在线托管时 PHP 文件的 JSON 响应出错

ios - DACircularProgress Track Circle 大于 Progress Circle

android - 跨平台的 WebView 。架构师观点?