简体   繁体   English

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

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

I want to get a time distance from firestore and there are 93 documents so this function takes few minutes to go through all the documents我想获得与 Firestore 的时间距离,并且有 93 个文档,所以这个 function 需要几分钟才能通过所有文档到达 go

and this is my code for the calculation -这是我的计算代码 -

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

So are there any efficient way to do this?那么有没有有效的方法来做到这一点? if someone can help me it would be great:)如果有人可以帮助我,那就太好了:)

You are retrieving the documents inside the collection nameDetails twice which is not really needed.您正在两次检索集合nameDetails中的文档,这并不是真正需要的。 If you just want to retrieve the distance, you can just do the following:如果您只想检索距离,您可以执行以下操作:

          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