繁体   English   中英

Flutter:按日期读取和合并 firestore 数据

[英]Flutter: Reading and merging firestore data by date

我正在寻找一种方法来按日期合并我从 Cloud Firestore 读取的数据。 我的目标是从 Cloud Firestore 读取所有文档,然后将特定日期的所有文档放在 ONE ExpansionTile下。 即每个日期一个ExpansionTile ,每个ExpansionTile可能包含许多单独的文档,具体取决于当天上传的文档数量。

我在从 Cloud Firestore 读取数据时没有问题,但是一旦掌握了手头的信息,我就很难弄清楚如何按日期组织和合并文档。 请记住,会有多个不同的日期,每个日期都有不同数量的文件。

如下图所示,我目前有三个具有相同日期的文档,因此在应用程序中创建它们时有三个不同的ExpansionTiles 我希望将这三个转换为一个ExpansionTile

在此处输入图像描述

我目前正在使用flutter_bloc package 按日期从 Cloud Firestore 读取数据:

    //Retrieves firebase data of all orders done in the past
  Stream<OrderHistoryState> _loadOrderHistory() async* {
    try {
      _orderSubscription?.cancel();

      var orderHistory = Firestore.instance
          .collection('orders')
          .orderBy('date', descending: true);

      _orderSubscription = orderHistory.snapshots().listen(
        (event) {
          if (event.documents.length > 0) {
            add(OrderHistoryUpdate(
              documents: event.documents,
            ));
          } else {
            print("No Order to Load");
            add(NoOrderHistory());
          }
        },
      );
    } catch (e) {
      print("Error with Tracking Status, $e");
          add(ErrorOrderHistory());
        }  
}

对于那些可能好奇的人,这是我对问题的解决方案。 它可能有点冗长,所以任何缩短/改进它的建议仍然非常感谢:

                  //Make sure that there are documents within firebase
                  if (state.orderHistoryDocuments.length != 0) {
                    //Adding the first document manually so that
                    //the forloop section has something to compare dates
                    //This listForEachDate is for storing a collection
                    //of all documents of one date
                    listForEachDate = [state.orderHistoryDocuments[0].data];
                    //listOfDateLists is a list of all listForEachDate
                    //This gives us a list of lists with the documents
                    //separated by date i.e.
                    //[[date1, date1], [date2], [date3, date3, date3], etc]
                    listOfDateLists = [];

                    //i = 1 because index 0 already added above
                    for (int i = 1;
                        i < state.orderHistoryDocuments.length;
                        i++) {
                      //If the current index's date matches that of the previous
                      //index's date, then add it to the listForEachDate
                      if (state.orderHistoryDocuments[i]
                              .data["dateCreated"] ==
                          state.orderHistoryDocuments[i - 1]
                              .data["dateCreated"]) {
                        listForEachDate
                            .add(state.orderHistoryDocuments[i].data);
                        //If [index]date does not match [index - 1]date
                        //Then add the current listForEachDate to the
                        //listOfDateLists i.e. add sublist to list of lists
                      } else {
                        listOfDateLists.add(listForEachDate);
                        //Clear the listForEachDate so that we can create
                        //a new clean list of new dates
                        listForEachDate = [];
                        //Add the new date to the listForEachDate
                        //so that the process can start again
                        listForEachDate
                            .add(state.orderHistoryDocuments[i].data);
                      }
                    }
                    //Once the document has been iterated through,
                    //Add to the big list.
                    listOfDateLists.add(listForEachDate);
                  }

暂无
暂无

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

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