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

Flutter洗衣App报错:(DateTime)=>String无法转为DateTime类型

问题解决:DateTime转字符串类型不匹配错误

错误根源

错误type '(DateTime) => String' is not a subtype of type 'DateTime' in type cast的直接原因是AppFormat类的date方法存在笔误:

原代码中,方法参数定义为dateTime,但内部调用format时误用了未定义的变量date,还多余添加了类型强转as DateTime,直接导致类型匹配失败。

修复方案

将AppFormat的date方法修正为使用正确的参数变量:

import 'package:intl/intl.dart';

class AppFormat {
  static String date(DateTime dateTime) {
    // 修复:使用方法参数dateTime,移除多余的类型强转
    return DateFormat('EEEE , d MMM yyyy').format(dateTime);
  }

  static String time(DateTime dateTime) {
    return DateFormat('HH:mm').format(dateTime);
  }

  static String currency(double price) {
    return NumberFormat.currency(
      decimalDigits: 2,
      symbol: '\$',
      locale: 'en_US',
    ).format(price);
  }
}

额外验证

其他代码部分的DateTime转字符串逻辑均正常:

  • DetailLaundry中AppFormat.date(laundry.queueDate!)的调用方式符合规范
  • WhereStatusPage中DateFormat('yyyy-MM-dd').format(laundry.queueDate!)的写法无问题

修复后重新运行App,类型不匹配的错误即可解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 01:07:51