Flutter-重新订购存储在FireStore中的数据

问题描述

enter image description here

如您所见,我正在使用Flutter的ReorderableListView小部件拖动任务。

onReorder能够上下拖动任务。但是,当我关闭该应用程序时,所有任务将按照其安排的认顺序进行。

这是因为从CloudFireStore提取的数据没有改变其顺序,而是通过拖动来更新它的方式。

任何人都可以帮助我,如何更新在Cloud FireStore中存储为文档的任务的位置,这样,当我关闭应用程序并再次打开它时,它会显示新的更新位置而不是旧的位置任务的位置

enter image description here

代码

child: StreamBuilder(
                    stream: FirebaseFirestore.instance
                        .collection("Tasks")
                        .doc(_email)
                        .collection("User_Tasks_List")
                        .snapshots(),builder: (context,snapshot) {
                      if (!snapshot.hasData) return const Text("Loading!...");
                      _docs = snapshot.data.documents;
                      return Theme(
                        data: ThemeData(canvasColor: Colors.transparent),child: ReorderableListView(
                            children: _docs
                                .map((e) => InkWell(
                                    key: ObjectKey(e),onTap: () => _popupDialog(context,e),onDoubleTap: () => FirebaseFirestore
                                            .instance
                                            .runTransaction(
                                                (transaction) async {
                                          transaction.delete(e.reference);
                                          Fluttertoast.showToast(
                                              msg: "Task has been deleted!");
                                        }),child: ViewHolder(e)))
                                .toList(),onReorder: onReorder),);
                    }),

解决方法

您可以向Firestore中的项目添加类似EVT_WDF_TIMER 的参数。然后,您需要保存已排序列表项的顺序,并使用索引将它们存储在Firestore中。

获取项目时,需要按此索引对它们进行排序,您将获得相同的顺序。

index:x