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

Flutter中Closure call with mismatched arguments报错的修复咨询

问题解决:Flutter交易添加按钮参数不匹配错误

错误原因

报错信息明确显示:调用_addNewTransaction时传入了3个参数(字符串、数值、DateTime实例),但该函数的定义仅接受2个参数,导致参数不匹配触发异常。

解决方案

1. 更新_addNewTransaction函数定义

在你的_MyHomePageState类中,找到_addNewTransaction函数,修改参数列表,新增DateTime类型的日期参数:

void _addNewTransaction(String title, double amount, DateTime date) {
  final newTx = Transaction(
    id: DateTime.now().toString(),
    title: title,
    amount: amount,
    date: date, // 新增日期字段
  );

  setState(() {
    _userTransactions.add(newTx);
  });
}

如果尚未定义交易数据类,需先创建包含日期字段的Transaction类:

class Transaction {
  final String id;
  final String title;
  final double amount;
  final DateTime date;

  Transaction({
    required this.id,
    required this.title,
    required this.amount,
    required this.date,
  });
}

2. 确保传递正确的函数给NewTransaction

在MyHomePage的build方法中,创建NewTransaction组件时,传递更新后的_addNewTransaction函数:

// 示例:FloatingActionButton触发弹窗的代码
FloatingActionButton(
  onPressed: () => showModalBottomSheet(
    context: context,
    builder: (_) => NewTransaction(_addNewTransaction),
  ),
  child: Icon(Icons.add),
),

3. 验证日期保存逻辑

此时_submitData方法中传递三个参数的代码可正常运行:

widget.addTx(
  enteredTitle,
  enteredAmount,
  _selectedDate,
);

修改完成后,点击添加交易按钮即可同时保存标题、金额和日期,不会再出现参数不匹配的错误,交易记录也能正确关联并显示日期信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 19:50:17