简体   繁体   English

为什么FocusNode需要在flutter中进行配置?

[英]Why FocusNode needs to dispose in flutter?

I am kinda very confused when to use dispose in flutter.我很困惑何时在颤振中使用dispose Are there any good tutorials about this?有没有关于这方面的好教程?

In the cookbook https://flutter.io/docs/cookbook/forms/focus :在食谱https://flutter.io/docs/cookbook/forms/focus 中

void dispose() {
  // Clean up the focus node when the Form is disposed
  myFocusNode.dispose();

  super.dispose();
}

Why do I have to call myFocusNode.dispose();为什么我必须调用myFocusNode.dispose(); ? ? What happened if I don't call it?如果我不打电话会怎样?

Please explain a bit clear or give me some obvious bad code examples showing bad results when not to call dispose for FocusNode or other situations instead of simply telling me this will cause memory leak...请解释清楚一点或给我一些明显的错误代码示例,当不为 FocusNode 调用 dispose 或其他情况而不是简单地告诉我这会导致内存泄漏时,会显示不良结果...

Thanks a lot.非常感谢。

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.例如,如果一个有状态的widget有一个可聚焦的子widget,它应该在State.initState方法中创建一个FocusNode,并在State.dispose方法中处置它,每次State.build方法都为可聚焦的子widget提供相同的FocusNode正在运行。 In particular, creating a FocusNode each time State.build is invoked will cause the focus to be lost each time the widget is built.特别是,每次调用 State.build 时创建一个 FocusNode 将导致每次构建小部件时失去焦点。

In other words, disposing them ensures that the node invokes focus correctly each time the widget is built, there are no issues with other Widgets which invoke focus and it also preserves performance.换句话说,处理它们确保节点在每次构建小部件时正确调用焦点,其他调用焦点的小部件没有问题,并且还保留了性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM