簡體   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