Flutter-在onTap()上删除Firebase文档

虚幻

我有一个ListView,其中充满了Firebase集合作为聊天消息。

当用户长按ListTile(列表中的一项)时,我将显示BottomSheet。

当用户单击DeleteBottomSheet上按钮时,应删除该文档。点击“删除”按钮后,我找不到删除所选文档的方法。

listview小部件:

return ListView.builder(
    padding: new EdgeInsets.all(8.0),
    reverse: true,
    itemCount: snapshot.data.documents.length,
    itemBuilder: (_, int index) {
    var message = snapshot.data.documents[index]["content"];
    var from = snapshot.data.documents[index]["idFrom"];
    return new ListTile(
        leading: new CircleAvatar(
            backgroundColor: Colors.blue,
            child: new Image.network(
                "http://res.cloudinary.com/kennyy/image/upload/v1531317427/avatar_z1rc6f.png")),
        title: new Row(
        children: <Widget>[
            new Expanded(child: new Text(message))
        ]
        ),
        subtitle: new Text(from),
        onLongPress: () {
        showModalBottomSheet<void>(context: context,
            builder: (BuildContext context) {
                return Container(
                    child: new Wrap(
                    children: <Widget>[
                        new ListTile(
                        leading: new Icon(Icons.delete),
                        title: new Text('Delete'),
                        onTap: () =>
                                          // Remove the tapped document here - how?
                                          print(snapshot.data.documents[index]["id"])
                                          Firestore.instance.collection("chats").document("ROOM_1")
                                          .collection("messages").document(snapshot.data.documents[index]["id"])
                                          .delete();
                                      )
                        )
                    ]
                    )
                );
            });
        }
    );
});

我需要找到一种方法来删除文档中的文件onTap()不知道为什么,但是即使我这样做:

 onTap: () => {
     // Remove the tapped document here - how?
     const id = snapshot.data.documents[index]["id"];
 },

IDE重新运行错误 Expected an identifier

杰罗姆·埃斯卡兰特

要删除的文件,我们可以使用runTransaction的方法Firestore.instance和使用delete该方法Transaction的类。

await Firestore.instance.runTransaction((Transaction myTransaction) async {
    await myTransaction.delete(snapshot.data.documents[index].reference);
});

代替

Firestore.instance.collection("chats").document("ROOM_1")  
    .collection("messages").document(snapshot.data.documents[index]["id"])
    .delete();

这是删除doc1文档之前我的虚拟数据库的外观

在此处输入图片说明

删除后doc1

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从android连接firebase上的文档(flutter)

在Flutter中在onTap上显示全屏图像

从Firebase Flutter获取文档ID

容器的Flutter onTap方法

Flutter googleMap onTap longPress

Flutter onPressed()与onTap()

如何以编程方式模拟 Flutter 中按钮上的 onTap?

如何使用firebase firestore flutter的自动生成的ID删除集合中的特定文档?

在flutter中从cloud_firestore删除文档

Flutter Firebase通过ID数组获取文档

Flutter Firebase-从文档获取特定字段

Flutter StreamBuilder用于多个Firebase文档

iOS 上的 Firebase 设置(Flutter)

Flutter OnTap 列表视图项

Flutter在`flutter`命令上失败

如何用flutter删除Firebase存储文件?

Flutter 删除 ListView.builder 中的一个项目 onTap of a button

为什么在删除onTap和OtherCallBacks之后此Flutter代码不起作用

在Flutter中删除导航上的覆盖

如何在 Flutter 上获取 Cloud Firestore 上的文档 ID

如何在 Flutter Firebase 中查询 id isEqualTo currentUser uid 的多个文档?- Flutter Firebase

Flutter 我们可以在单个 TextField 上同时使用 ontap 和 onchanged 属性吗?

Android Studio / Flutter / Nexus 6 API 30 上的 GestureDetector OnTap 延迟

如何使用flutter从Cloud Firestore中删除特定文档?

如何用flutter删除Firestore文档中的字段

Flutter-从Cloud Firestore中的文档中删除特定字段?

按 Firestore 中字段的内容删除文档(flutter)

Firebase 未检索 Flutter 项目中的嵌套集合文档

如何在flutter中更新Firebase中的收集文档?