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

Flutter/Dart报错:命名参数'id'未定义及参数不匹配求助

问题修复方案

你的错误根源在transaction.dart的类定义上,以下是具体问题和修复代码:

存在的问题

  • DateTime字段缺失变量名,正确写法应为final DateTime date;
  • 构造函数定义的是位置参数,但你实例化时用的是命名参数(id:这类形式),导致参数匹配失败
  • 构造函数里的this.DateTime是错误写法,需对应字段名this.date

修复后的transaction.dart代码

import 'package:flutter/foundation.dart';

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,
  });
}

说明

将构造函数改成带大括号的命名参数形式后,你在main.dart里用id: 't1'这类命名参数实例化的写法就能正常运行,同时修正了字段名与构造函数的对应关系,所有报错都会消失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 00:42:02