首页 > 解决方案 > 是否可以根据 dateTime 值从 firebase 查询文档到颤动?

问题描述

我试图以两种方式从我的firestore查询文档到我的flutter应用程序,第一种是当我的“Patients”集合中的“translator”字段为='$ currentUser'时获取所有文档,如下所示并且工作正常:

FirebaseFirestore.instance
        .collection('Patients')
        .where('translator', isEqualTo: _auth.currentUser!.displayName)
        .snapshots()

接下来我要做的是根据“到达日期”字段查询数据,该字段是 DateTime 格式化为字符串,如图所示

图片链接

我怎样才能得到当前日期时间在到达日期之前的所有文件,像这样

 FirebaseFirestore.instance
            .collection('Patients')
            .where('arrivalDate' , '????': arrivalDate.isBefore(DateTime.now())

标签: firebaseflutter

解决方案


当然,您只需要将日期格式设置为与文档中相同的格式。

像这样的东西:

import 'package:intl/intl.dart';
...
String formattedDate = DateFormat('yyyy-MM-dd').format(now);

FirebaseFirestore.instance
            .collection('Patients')
            .where('arrivalDate' , isLessThan: formattedDate)

推荐阅读