[英]Do all widgets in Flutter have the "bool this.mounted" property?
Peace be upon you祝你平安
I am researching Flutter's widget lifecycle and stopped at a point which is the this.mounted
boolean variable, it does exist directly in the State
class, which can only be connected with a StatefulWidget
as a subclass and never will/did with a StatelessWidget
.我正在研究 Flutter 的小部件生命周期,并在
this.mounted
boolean 变量处停止,它确实直接存在于State
class 中,它只能作为子类与StatefulWidget
连接,永远不会/与StatelessWidget
连接。
The question is...
问题是...
Do all widgets actually contain the
bool this.mounted
variable as some say here in [ this accepted answer , this article and this ], or not?是否所有小部件实际上都包含
bool this.mounted
变量,正如某些人在 [ this accepted answer , this article and this ] 中所说的那样?
I searched in Flutter and found this mounted variable only in the State
class, then I looked further to find a function called mount()
inside the Element
class.我在Flutter中查找,只在
State
class中找到了这个挂载变量,再往下找,在class元素中找到了一个名为mount()
的Element
。
The StatelessWidget
class is connected to the StatelessElement
class, which extends from the ComponentElement
that inherits directly from the Element
class. StatelessWidget
class 连接到StatelessElement
class,它从直接继承自Element
class 的ComponentElement
扩展而来。
I'm really confused right now, maybe I misunderstood this, I need an explanation for this please,.我现在真的很困惑,也许我误解了这一点,我需要对此进行解释,。 Thank you.
谢谢你。
In the future versions of Flutter (currently 3.3.4
on the stable channel) BuildContext
will have a getter bool mounted
so you can use it in stateless widgets:在 Flutter 的未来版本中(目前稳定通道上的
3.3.4
) BuildContext
将安装一个 getter bool mounted
因此您可以在无状态小部件中使用它:
See this PR which adds the getter: https://github.com/flutter/flutter/pull/111619请参阅此添加吸气剂的 PR: https://github.com/flutter/flutter/pull/111619
This has been released with Flutter 3.7.0
.这已与 Flutter
3.7.0
一起发布。 You can now use context.mounted
in stateful and stateless widgets.您现在可以在有状态和无状态小部件中使用
context.mounted
。
Not sure why that answers says all widgets
.不知道为什么答案说
all widgets
。 But NO
.但是
NO
。
Only State<T>
objects are the ones that have a getter
named mounted
.只有
State<T>
对象是具有名为mounted
的getter
的对象。
This is easy to verify, Just cmd + click
or ctrl + click
on any State
you have in your class which will redirect you to a file called framework.dart
which is located at flutter -> package -> flutter -> lib -> src -> widgets
path. This is easy to verify, Just
cmd + click
or ctrl + click
on any State
you have in your class which will redirect you to a file called framework.dart
which is located at flutter -> package -> flutter -> lib -> src -> widgets
路径。
Search mounted
and this line is the only actual declaration that you will find.搜索已
mounted
,此行是您将找到的唯一实际声明。
bool get mounted => _element != null;
And that's part of the State
class' code.这是
State
类代码的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.