扑朔迷离的带有timestemp的Firestore查询

时间:2020-03-30 07:12:23

标签: flutter google-cloud-firestore nosql timestamp

我正在构建一个应用,我需要在其中获取今天计划的任务列表。但是我不确定如何编写查询。

这是我现在拥有的查询,它提供了从当前时间到无限远的任务列表。但是我希望任务只有今天的日期。

  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: Firestore.instance.collection('tasks').where(
          'uid', isEqualTo: "$uid").where('time', isGreaterThanOrEqualTo: DateTime.now()).snapshots()

1 个答案:

答案 0 :(得分:0)

如果仅需要一天的文档,则需要一个查询,查询的范围是一天的开始和结束。在Flutter中,您可以执行以下操作:

Widget build(BuildContext context) {
    DateTime now = DateTime.now();
    DateTime start = now.subtract(Duration(hours: now.hour, minutes: now.minute, seconds: now.second));
    DateTime end = start.add(Duration(days: 1));
    return StreamBuilder<QuerySnapshot>(
      stream: Firestore.instance.collection('tasks').where(
          'uid', isEqualTo: "$uid").where('time', isGreaterThanOrEqualTo: start).where('time', isLessThanOrEqualTo: end).snapshots()