我需要使用 Flutter 在 Firestore 中使用嵌套数组更新文档。
所以我需要将完整的文档放入一个 Map 中,重新排序“sections”数组中的映射,然后将数据存储回文档中。
然而,我不熟悉如何将快照 (DocumentSnapshot) 的数据放入地图。
下面是一个对我尝试实现的目标不起作用的示例:
final Map<String, dynamic> doc = snapshot.data as Map<String, dynamic>;
Run Code Online (Sandbox Code Playgroud)
“snapshot.data”包含文档的值。文档的结构如下所示:
{
name: "Course 1"
sections: [
{name: "Section 1"},
{name: "Section 2"}
]
}
Run Code Online (Sandbox Code Playgroud)
重新排序部分数组中的地图后,我需要将数据保存回文档中。
这里功能齐全。相关代码在“onDragFinish”中。
// Build editable list with draggable list tiles and swipe to delete
List<Widget> buildListViewEdit() {
final course = db.collection("school").document("3kRHuyk20UggHwm4wrUI")
.collection("course").document("74UsE9x7Bsgnjz8zKozv").snapshots();
return [
StreamBuilder(
stream: course,
builder: (context, snapshot) {
if (!snapshot.hasData) return const Text("Loading...");
return Expanded(
child: DragAndDropList( …Run Code Online (Sandbox Code Playgroud) 我正在构建一个 flutter 应用程序并使用 cloud-firestore,这就是我的数据库的样子

我想要一个函数,它在一个字符串数组中检索名为“驱动程序列表”的集合中的所有文档,这些文档是我已经使用过的,但它会将它们返回到新屏幕的列表视图中
class DriverList extends StatelessWidget {@overrideWidget build(BuildContext context) {
return new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('DriverList').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView(
children: snapshot.data.documents.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document['name']),
subtitle: new Text(document['phone']),
);
}).toList(),
);
},
);
Run Code Online (Sandbox Code Playgroud)
} }