flutter - Flutter 中的 FocusNode 'node.ancestors.contains(this)' 错误
问题描述
TextEditingController _currentPasswordController = TextEditingController();
TextEditingController _newPasswordController = TextEditingController();
FocusNode _currentPasswordFocus = FocusNode();
FocusNode _newPasswordFocus = FocusNode();
//---------------------------------------------------
PasswordField(
controller: _currentPasswordController,
autoFocus: true,
focusNode: _currentPasswordFocus,
maxLength: 20,
),
PasswordField(
autoFocus: false,
focusNode: _newPasswordFocus,
maxLength: 20,
onSaved: (newValue) {
setState(() {
newPassword = newValue;
});
},
controller: _newPasswordController,
)
//Save Button-->
onPressed: () {
if (formKey.currentState.validate()) {
formKey.currentState.save();
if (_currentPasswordController.text != _newPasswordController.text) {
FocusScope.of(context).autofocus(_currentPasswordFocus);
..................
......................
}
}
如果在上面的代码示例中按下按钮时文本表单字段值不相等,我希望光标聚焦在 _currentPasswordController 字段上。
它在发布模式下的真实设备上运行良好。但是我在调试模式下在模拟器上收到此错误消息。
Autofocus was requested for a node that is not a descendant of the scope from which it was requested.
'Package: flutter / src / widgets / focus_manager.dart':
Failed assertion: line 1024 pos 14: 'node.ancestors.contains (this)'
解决方案
推荐阅读
- sql - Oracle Apex 20.1 错误:处理验证时出错
- c# - 从数据库加载图像而不使用 c# 中的处理程序
- javascript - 在类和 JavaScript 模块中使用反射
- php - 尝试在 iis 上部署 wordpress 时出现 mod 安全错误
- html - 如何在移动版本中使两个 div 水平和两个 div 垂直?
- c# - Parseplatform .Net 核心
- php - 如何在 PHP 中获取特定值的字符串数组?
- event-listener - 如何在 ActivitiEventListener 中获取任务
- javascript - 如何在容器内显示滑块并结束直到视口
- python-3.x - 使用 AWS EMR 调用 MongoDB Atlas 时出现连接超时异常