【问题标题】:Flutter: Reading and merging firestore data by dateFlutter:按日期读取和合并 Firestore 数据
【发布时间】:2021-01-08 08:58:49
【问题描述】:

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

我在从 Cloud Firestore 读取数据时没有遇到任何问题,但我正在努力弄清楚如何在掌握信息后按日期组织和合并文档。请记住,会有多个不同的日期,并且每个日期都有不同数量的文档。

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

我目前正在使用 flutter_bloc 包按日期从 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());
        }  
}

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-01-12
      • 1970-01-01
      • 2020-10-20
      • 2021-11-08
      • 1970-01-01
      • 2020-02-03
      • 2018-10-01
      • 1970-01-01
      • 2020-04-19
      相关资源
      最近更新 更多