【问题标题】:Flutter - Filter Stream based on Date pickedFlutter - 根据选择的日期过滤流
【发布时间】:2020-11-19 15:10:09
【问题描述】:

我有一个简单的颤振应用程序,我从 Firebase 检索一堆记录。记录中包含一个名为 DATE 的字段,格式为 DateTime。(Date + time + TZ)

但是从我的应用程序中,我怎样才能创建一个页面,我可以在其中过滤仅针对选定 DATE 的记录。 当我使用 .where('time', isGreaterThan : _selectedDate) 时,它可以工作。但它给出了所选日期之后的所有天数。我只想过滤一个选定的日期。

这是我的代码:

Stream<QuerySnapshot> _getCurrentOders() async* {

   yield* FirebaseFirestore.instance
        .collection('ItemList')
        .where('time', isGreaterThan: _selectedDate)
        .orderBy('time', descending: false)
        .snapshots();
  }

我还使用 DateTime 选择器来选择日期。

DateTimeField(
              initialValue: DateTime.now(),
              onChanged: (val) {
                setState(() {
                  _selectedDate = val;
                });
               
              },
              format: format,
              onShowPicker: (context, currentValue) {
                return showDatePicker(
                    context: context,
                    firstDate: DateTime(2019),
                    initialDate: currentValue ?? DateTime.now(),
                    lastDate: DateTime(2100));
              },
            ),

感谢您的支持!

【问题讨论】:

    标签: flutter dart


    【解决方案1】:
    // this gives you the first millisecond of the day    
    var startOfTheDay = DateTime(_selectedDate.year, _selectedDate.month, _selectedDate.day);
    
    //and this gives you the first millisecond of the next day   
    var endOfTheDay = startOfTheDay.add(Duration(days: 1);
    

    然后你可以使用:

    .where('time', isGreaterThan : startOfTheDay).where('time', isLessThan : endOfTheDay)
    

    【讨论】:

    • 哎呀...从来没想过!非常感谢你。它有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2023-03-17
    • 2018-05-17
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    相关资源
    最近更新 更多