什么时候在 flutter 中使用 dispose
我有点困惑。有没有关于这个的好教程?
在食谱中 https://flutter.io/docs/cookbook/forms/focus :
void dispose() {
// Clean up the focus node when the Form is disposed
myFocusNode.dispose();
super.dispose();
}
为什么我必须调用 myFocusNode.dispose();
?如果我不打电话会发生什么?
请解释清楚,或者给我一些明显的错误代码示例,在不为 FocusNode 或其他情况调用 dispose 时显示错误结果,而不是简单地告诉我这会导致内存泄漏...
非常感谢。
最佳答案
Focus nodes are long-lived objects. For example, if a stateful widget has a focusable child widget, it should create a FocusNode in the State.initState method, and dispose it in the State.dispose method, providing the same FocusNode to the focusable child each time the State.build method is run. In particular, creating a FocusNode each time State.build is invoked will cause the focus to be lost each time the widget is built.
换句话说,处理它们可确保每次构建小部件时节点正确调用焦点,其他调用焦点的小部件没有问题,并且还保留了性能。
关于dart - 为什么 FocusNode 需要在 flutter 中配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299240/