飞镖/扑动:日期范围内的天数列表 [英] Dart/Flutter : List of days in a date range

查看:30
本文介绍了飞镖/扑动:日期范围内的天数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从日期范围中检索天列表.用户选择开始日期和结束日期,并计算介于两者之间的天数(包括开始和结束时间).

I need to retrieve a list of days from a date range. The user selects the start date and the end date and it calculates the days in between (start and end included).

我尝试使用如下所示的for循环:

I tried to use a for loop as follow:

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

,但是它不起作用,它会冻结该应用程序.你们有什么建议吗?预先感谢.

but it doesn't work and it freezes the app. Do you guys have any suggestions? Thanks in advance.

推荐答案

如何:

final daysToGenerate = end.difference(start).inDays;
days = List.generate(daysToGenerate, (i) => DateTime(start.year, start.month, start.day + (i)));

这篇关于飞镖/扑动:日期范围内的天数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆