繁体   English   中英

如何获得从 Firestore 到 flutter 的时间距离

[英]How to get a time distance from firestore to flutter

我想获得与 Firestore 的时间距离,并且有 93 个文档,所以这个 function 需要几分钟才能通过所有文档到达 go

这是我的计算代码 -

  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;
            }
          }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM