我需要从日期范围中检索天数列表。用户选择开始日期和结束日期,并计算两者之间的天数(包括开始和结束)。
我尝试使用 for 循环如下:
List<DateTime> calculateDaysInterval(DateTime startDate, DateTime endDate) {
List<DateTime> days = [];
for (DateTime d = startDate;
d.isBefore(endDate);
d.add(Duration(days: 1))) {
days.add(d);
}
return days;
}
但它不起作用并且它卡住了应用程序。
大家有什么建议吗?
提前致谢。
最佳答案
怎么样:
final daysToGenerate = end.difference(start).inDays;
days = List.generate(daysToGenerate, (i) => DateTime(start.year, start.month, start.day + (i)));
关于flutter - Dart/flutter : List of days in a date range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57422219/