【问题标题】:Flutter Filter List base on Month基于月份的 Flutter 过滤器列表
【发布时间】:2021-10-16 18:58:11
【问题描述】:

如何根据月份对列表进行分类

假设我有如下示例列表:

Record(
    id: '01',
    nameFood: 'ravioli',
    date : 2021/may/14,
    price: 20
    
  ),
Record(
    id: '02',
    nameFood: 'cheese garlic',
    date : 2021/june/02,
    price: 30,
  ),
Record(
    id: '03',
    nameFood: 'steak',
    date : 2021/march/21
    price 25,
  ),

我想把它归类为喜欢这个

 June(
    nameFood: steak,
    total price: 240,
   ),

and etc

问题是我不知道如何过滤每个列表(月),因为每个月都有不同的天数,例如 30,31 和 28

【问题讨论】:

    标签: list flutter sorting dart


    【解决方案1】:

    如果您将日期存储为 DateTime,则可以尝试使用月份编号。假设你有一个 Record 类型的对象列表,Record 在哪里。

    class Record {
      Record(
        this.id,
        this.price,
        this.date,
        this.nameFood,
      );
    
      final String id;
      final double price;
      final DateTime date;
      final String nameFood;
    }
    

    你可以遍历你的列表,然后做任何你想做的事情,就像这样。

    for (final record in _listOfRecords) {
      final _monthOfRecord = record.date.month; // Gives the month number. January = 1, April = 4, etc.
      // Do other stuff, based on the current monthNumber
      switch (_monthOfRecord) {
        case 1:
          // Do something
          break;
        ....
        default:
          break;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-08
      • 2017-11-16
      • 2013-09-06
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      相关资源
      最近更新 更多