简体   繁体   中英

Flutter: Reading and merging firestore data by date

I am looking for a way to merge the data I'm reading from Cloud Firestore by date. My aim is to read all the documents from Cloud Firestore, then place all documents of a certain date under ONE ExpansionTile . ie One ExpansionTile per date, and each ExpansionTile may contain many individual documents depending on how much was uploaded that day.

I'm having no issues reading the data from Cloud Firestore, but I am struggling to figure out how to organise and merge the documents by date once I have the information at hand. Keeping in mind that there will be multiple different dates and all with different amounts of documents per date.

As seen in the picture below, I currently have three documents with the same date, and therefore three different ExpansionTiles when creating them in the app. I am looking to convert these three into one ExpansionTile

在此处输入图像描述

I am currently reading the data from Cloud Firestore by date using the flutter_bloc package:

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

For those of you who may be curious, this was my solution to the problem. It may be a bit long winded, so any suggestions to shorten/improve it are still greatly appreciated:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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