Flutter自定义邮箱登录触发getSelectedText on inactive InputConnection错误
Flutter自定义邮箱登录InputConnection警告排查
报错详情
W/IInputConnectionWrapper( 5518): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper( 5518): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper( 5518): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper( 5518): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper( 5518): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper( 5518): endBatchEdit on inactive InputConnection
错误原因
- 页面销毁时没有主动释放
TextEditingController和FocusNode资源,输入连接已经失效后系统仍然尝试访问输入框状态 - 密码输入框的
onEditingComplete回调没有正确执行提交逻辑,焦点没有正常释放,持续持有无效输入连接 - 提交表单后直接执行页面跳转,没有提前取消输入框焦点,页面销毁时输入框仍然处于激活状态,连接被强制中断
- 切换登录/注册表单类型时,直接清空激活状态输入框的内容,触发系统对无效输入连接的访问
修复方案
1. 完善dispose方法,主动释放资源
修改当前的dispose实现,销毁所有控制器和焦点节点:
@override void dispose() { print('dispose called'); _emailController.dispose(); _PasswordController.dispose(); _emailFocusNode.dispose(); _passwordFocusNode.dispose(); super.dispose(); }
2. 修正密码输入框的onEditingComplete逻辑
调整回调写法,提交前先取消输入框焦点:
TextField _buildPasswordTextField() { return TextField( controller: _PasswordController, focusNode: _passwordFocusNode, decoration: InputDecoration( labelText: 'password', errorText: model.passwordErrorText, enabled: model.isloading == false, ), textInputAction: TextInputAction.done, obscureText: true, onChanged: widget.model.updatePassword, onEditingComplete: () { FocusScope.of(context).unfocus(); _submit(); }, ); }
3. 表单提交前先取消全局焦点
调整_submit方法逻辑,所有操作执行前先断开输入连接:
Future<void> _submit() async { FocusScope.of(context).unfocus(); print('submit called'); print(' email: ${_emailController.text} password: ${_PasswordController.text}'); try { await model.submit(); } catch (e) { print(e.toString()); print('in e'); showDialog( context: context, builder: (context) { return AlertDialog( title: Text('登录失败'), content: Text(e.toString()), actions: [TextButton(onPressed: () => Navigator.of(context).pop(), child: Text('确定'))], ); }, ); return; } Navigator.of(context).pop(); }
4. 切换表单类型前先取消焦点
调整_toggleFormType方法,清空内容前先断开输入连接:
void _toggleFormType() { FocusScope.of(context).unfocus(); widget.model.toggleFormType(); _emailController.clear(); _PasswordController.clear(); }
注意事项
该警告属于Android原生输入服务的提示,本身不会导致应用崩溃,完成上述修改后即可完全消除警告。
内容的提问来源于stack exchange,提问作者priyam Gupta
相关产品推荐
相关产品推荐

