firebase - 如何获得从消防站到 flutter 的时间距离

标签 firebase datetime flutter dart google-cloud-firestore

我想和消防站保持一段距离
共有93个文件
因此此功能需要几分钟来浏览所有文档

这是我的计算代码-

  int length;
  DateTime earlyTime;
  DateTime lateTime;
  String name;
  String finalName;

  QuerySnapshot snaps =
      await Firestore.instance.collection('nameDetails').getDocuments();
  List<DocumentSnapshot> snapCount = snaps.documents;
  length = snapCount.length;
  // Count of Documents in Collection

  for (int count = 1; count < length; count++) {
    print(count);
    await Firestore.instance
        .collection('nameDetails')
        .document(count.toString())
        .get()
        .then(
      (time) {
        //Parsing the strings into DateTime
        earlyTime = DateTime.parse(time['beforeTime']);
        lateTime = DateTime.parse(time['afterTime']);
        name = time['name'];
        print(name);
      },
    );
    if (birthDayTime.isAfter(earlyTime) && birthDayTime.isBefore(lateTime)) {
      name = finalName;
      break;
    }
  }
} 

那么,有什么有效的方法可以做到这一点吗?
如果有人可以帮助我,那就太好了:)

最佳答案

您正在检索nameDetails集合内的文档两次,而这并不是真正需要的。如果只想获取距离,则可以执行以下操作:

          QuerySnapshot snaps =
              await Firestore.instance.collection('nameDetails').getDocuments();
          for (var docs in snaps.documents) {
            earlyTime = DateTime.parse(docs['beforeTime']);
            lateTime = DateTime.parse(docs['afterTime']);
            name = docs['name'];
            print(name);
            if (birthDayTime.isAfter(earlyTime) &&
                birthDayTime.isBefore(lateTime)) {
              name = finalName;
              break;
            }
          }

关于firebase - 如何获得从消防站到 flutter 的时间距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61453373/

相关文章:

android - 如何获取智能横幅的横幅尺寸?

ios - Fabric 回答舍入 Int 值

android - 使用唯一标识符flutter/android识别设备

javascript - Dygraphs:适用于 axisLabelFormatter 但不适用于 valueFormatter 的 JS 函数

python - 使用Python仅从时间戳返回新列中的分钟

perl - 为什么在没有引号的情况下,Perl 中的 IF 条件下无法解析数字

java - 当您的应用程序通过通知打开时,是否可以从打开的位置返回到上一个应用程序?安卓

dart - 如何创建包含列表值的多维列表和循环小部件?

swift - FirebaseCrashlytics/运行 : No such file or directory error while building the app - swift

ios - 如何使用 Firebase 分发 iOS 应用程序?