问题描述
愿你平安
我正在研究 Flutter 的小部件生命周期并停在 this.mounted
布尔变量处,它确实直接存在于 State
类中,只能与 StatefulWidget
连接作为子类,永远不会/不会使用 StatelessWidget
。
问题是……
所有小部件是否真的像某些人在 [this accepted answer、this article 和 this ] 中所说的那样包含 bool this.mounted
变量?
我在 Flutter 中搜索,只在 State
类中找到了这个 mounted 变量,然后我进一步查找在 mount()
类中找到了一个名为 Element
的函数。
StatelessWidget
类连接到 StatelessElement
类,后者从 ComponentElement
扩展而来,Element
类直接继承自 private char[][] matrix;
public Matrix() {
matrix = new char[3][3];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
matrix[i][j] = '-';
}
}
}
// setter and getter
public void set(int rowIndex,int colIndex,char data) {
matrix[rowIndex][colIndex] = data;
}
public char get(int rowIndex,int colIndex) {
return matrix[rowIndex][colIndex];
}
// display the matrix
public void display() {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
System.out.print(matrix[i][j]);
}
System.out.println();
}
}
public static void println(String message) {
System.out.println(message);
}
public static void main(String[] args) {
Matrix matrix = new Matrix();
matrix.display();
matrix.set(0,'1');
matrix.println("");
matrix.display();
matrix.set(0,1,'2');
matrix.println("");
matrix.display();
}
类。
我现在真的很困惑,也许我误解了这一点,我需要解释一下!,谢谢。
解决方法
不知道为什么答案说 all widgets
。但是NO
。
只有 State<T>
对象是具有名为 getter
的 mounted
的对象。
这很容易验证,只需 cmd + click
或 ctrl + click
在您班级的任何 State
上,这会将您重定向到名为 framework.dart
的文件,该文件位于flutter -> package -> flutter -> lib -> src -> widgets
路径。
搜索 mounted
,这一行是您会找到的唯一实际声明。
bool get mounted => _element != null;
这是 State
类代码的一部分。