相关疑难解决方法(0)

在 Flutter 中将 Firestore DocumentSnapshot 转换为 Map

我需要使用 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)

重新排序部分数组中的地图后,我需要将数据保存回文档中。

  • 问题1:如何将snapshot.data 的内容读入Map?
  • 问题 2:我会删除文档,然后重新添加吗?或者我可以更新所有内容吗?

这里功能齐全。相关代码在“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 google-cloud-firestore

10
推荐指数
5
解决办法
3万
查看次数

如何在flutter中从firebase获取数据

我正在构建一个 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)

} }

dart firebase flutter google-cloud-firestore

8
推荐指数
2
解决办法
3万
查看次数

标签 统计

flutter ×2

google-cloud-firestore ×2

dart ×1

firebase ×1