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
相关产品推荐
相关产品推荐

