dart - 为什么 FocusNode 需要在 flutter 中配置?

标签 dart flutter

什么时候在 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 时显示错误结果,而不是简单地告诉我这会导致内存泄漏...

非常感谢。

最佳答案

As from the documentation:

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/

相关文章:

dart - Flutter/Dart 覆盖 getApplicationDocumentsDirectory 中的文件不能立即工作

html - Dart WebUI 构建错误

flutter - 无法将列表嵌套在ExpansionTileCard中

dart - 无法将AngularDart构建为JS

java - flutter 错误 : java. lang.NoSuchFieldError PREFER_HIGHEST_OR_REMOTE_VERSION_NO_FORCE_STAGING

json - 如何在 Flutter 中将响应 JSON 转换为对象?

flutter - 如何确定一个偏移量是否包含在另一个偏移量区域中?

flutter - ListView 生成器无法呈现任何内容

unit-testing - 有没有办法用 Mockito 在 Dart 中只模拟对象的一部分?

flutter :库比蒂诺 ListTile ? (或如何创建类似 iOS 的设置菜单)