Firebase 集合到 ReorderableListView

Firebase Collection into ReorderableListView

我在 Flutter 中有一个 Firebase 集合,我想将其包含在 reorderableListView 中。集合中的文档有一个 'position' 字段,该字段应确定列表的顺序并在列表重新排序时更新。

终于自己搞定了

onReorder: (oldIndex, newIndex) {
  setState(() {
    List<MyObjectClass> objectList =
        myObjects
            .data;
    if (newIndex > oldIndex) {
      newIndex -= 1;
    }

    final MyObjectClass
        myObject = objectList.removeAt(oldIndex);
    objectList.insert(newIndex, myObject);

    for (MyObjectClass o in objectList) {
      o.position = objectList.indexOf(o);
      DatabaseService()
          .updateMyObjects(
              o);
    }
  });
}

这是假设您在 DatabaseService() class 中有一个函数将 Firestore 集合映射到对象,然后在 StreamBuilder 中有 ReorderableListView 将所述对象的快照传递给它 'myObjects'.