You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter密码管理器Controller Listener调用updatePassword报错

Flutter密码管理器Firestore更新报错解决

问题背景

开发基于Flutter的密码管理器,使用Firestore存储title、email、userpassword、url四个字段的数据。四个输入控制器的监听器调用updatePassword方法时,反复出现“缺少必填命名参数”的错误,四类错误累计出现24次。

控制器监听器代码

void _titleControllerListener() async {
  final password = _password;
  if (password == null) {
    return;
  }
  final title = _titleController.text;
  await _passwordsService.updatePassword(
    documentId: password.documentId,
    title: title,
  );
}

void _emailControllerListener() async {
  final password = _password;
  if (password == null) {
    return;
  }
  final email = _emailController.text;
  await _passwordsService.updatePassword(
    documentId: password.documentId,
    email: email,
  );
}

void _userPasswordControllerListener() async {
  final password = _password;
  if (password == null) {
    return;
  }
  final userpassword = _userPasswordController.text;
  await _passwordsService.updatePassword(
    documentId: password.documentId,
    userpassword: userpassword,
  );
}

void _urlControllerListener() async {
  final password = _password;
  if (password == null) {
    return;
  }
  final url = _urlController.text;
  await _passwordsService.updatePassword(
    documentId: password.documentId,
    url: url,
  );
}

错误信息

The named parameter 'url' is required, but there's no corresponding argument.
Try adding the required argument.
The named parameter 'email' is required, but there's no corresponding argument.
Try adding the required argument.
The named parameter 'userpassword' is required, but there's no corresponding argument.
Try adding the required argument.
The named parameter 'title' is required, but there's no corresponding argument.
Try adding the required argument.

原updatePassword方法代码

Future<void> updatePassword({
  required String documentId,
  required String title,
  required String email,
  required String userpassword,
  required String url,
}) async {
  try {
    await passwords.doc(documentId).update({titleFieldName: title});
    await passwords.doc(documentId).update({emailFieldName: email});
    await passwords.doc(documentId).update({userpasswordFieldName: userpassword});
    await passwords.doc(documentId).update({urlFieldName: url});
  } catch (e) {
    throw CouldNotUpdatePasswordException();
  }
}

错误原因

  1. updatePassword方法中所有字段参数都标记为required,意味着每次调用必须传入全部四个字段,但监听器仅更新单个字段,导致参数缺失报错。
  2. 原方法对Firestore进行四次单独更新,不仅冗余,还可能引发并发问题。

修复方案

1. 修改updatePassword方法

将字段参数改为可选,仅收集需要更新的字段,一次性提交到Firestore:

Future<void> updatePassword({
  required String documentId,
  String? title,
  String? email,
  String? userpassword,
  String? url,
}) async {
  try {
    final updateData = <String, dynamic>{};
    if (title != null) updateData[titleFieldName] = title;
    if (email != null) updateData[emailFieldName] = email;
    if (userpassword != null) updateData[userpasswordFieldName] = userpassword;
    if (url != null) updateData[urlFieldName] = url;
    
    if (updateData.isNotEmpty) {
      await passwords.doc(documentId).update(updateData);
    }
  } catch (e) {
    throw CouldNotUpdatePasswordException();
  }
}

2. 保持监听器调用逻辑不变

原监听器的调用方式无需修改,因为现在方法允许仅传入单个需要更新的字段,不会再触发必填参数缺失的错误。

内容的提问来源于stack exchange,提问作者Ruben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 22:15:41