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

Flutter如何将毫秒时间戳列表转换为DateTime列表?

解决Flutter中List转List的类型错误

你当前的错误是因为直接将整个List<int>类型的datesSec传给了仅接受单个int参数的DateTime.fromMillisecondsSinceEpoch()方法,导致类型不匹配。要批量转换列表中的每个时间戳,需要遍历列表并逐个转换:

正确代码示例

// updatesDateEpoch is a list of int
List<int> datesSec = logRecord.updatesDateEpoch.asList();

// 遍历每个时间戳并转换为DateTime
List<DateTime> datesList = datesSec.map((millis) {
  return DateTime.fromMillisecondsSinceEpoch(millis);
}).toList();

或者写成更简洁的箭头函数形式:

List<DateTime> datesList = datesSec.map((millis) => DateTime.fromMillisecondsSinceEpoch(millis)).toList();

可选:指定时区

如果需要UTC时间,可以给转换方法添加isUtc参数:

List<DateTime> datesList = datesSec.map((millis) => DateTime.fromMillisecondsSinceEpoch(millis, isUtc: true)).toList();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 01:57:04