Flutter 颤振:按日期读取和合并firestore数据
我正在寻找一种方法,按日期合并从CloudFireStore读取的数据。我的目标是从CloudFireStore中读取所有文档,然后将某个日期的所有文档放在一个Flutter 颤振:按日期读取和合并firestore数据,flutter,google-cloud-firestore,Flutter,Google Cloud Firestore,我正在寻找一种方法,按日期合并从CloudFireStore读取的数据。我的目标是从CloudFireStore中读取所有文档,然后将某个日期的所有文档放在一个扩展文件下。i、 e.每个日期一个expansionfile,每个expansionfile可能包含许多单独的文档,具体取决于当天上载的文档数量 我从Cloud Firestore读取数据没有问题,但我很难弄清楚一旦掌握了信息,如何按日期组织和合并文档。请记住,将有多个不同的日期,每个日期都有不同数量的文档 如下图所示,我目前有三个日期相
扩展文件下。i、 e.每个日期一个expansionfile
,每个expansionfile
可能包含许多单独的文档,具体取决于当天上载的文档数量
我从Cloud Firestore读取数据没有问题,但我很难弄清楚一旦掌握了信息,如何按日期组织和合并文档。请记住,将有多个不同的日期,每个日期都有不同数量的文档
如下图所示,我目前有三个日期相同的文档,因此在应用程序中创建它们时有三个不同的扩展文件。我希望将这三个文件转换为一个expansionfile
我目前正在使用以下软件包按日期从Cloud Firestore读取数据:
//检索过去完成的所有订单的firebase数据
流\u loadOrderHistory()异步*{
试一试{
_orderSubscription?.cancel();
var orderHistory=Firestore.instance
.收款(“订单”)
.orderBy('date',降序:true);
_orderSubscription=orderHistory.snapshots()。侦听(
(活动){
如果(event.documents.length>0){
添加(OrderHistoryUpdate)(
文档:event.documents,
));
}否则{
打印(“无需加载订单”);
添加(NoOrderHistory());
}
},
);
}捕获(e){
打印(“跟踪状态错误,$e”);
添加(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);
}
//确保firebase中有文档
if(state.orderHistoryDocuments.length!=0){
//手动添加第一个文档,以便
//forloop部分可以比较日期
//此listForEachDate用于存储集合
//一个日期的所有文件的
listForEachDate=[state.orderHistoryDocuments[0].data];
//listOfDateLists是所有listForEachDate的列表
//这为我们提供了一个包含文档的列表
//按日期分开,即。
//[[日期1,日期1],[日期2],[日期3,日期3,日期3]等]
listOfDateLists=[];
//i=1,因为上面已经添加了索引0
对于(int i=1;
i
//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);
}