代码之家  ›  专栏  ›  技术社区  ›  TheUnreal

颤振-移除FireBase文档ontap()。

  •  0
  • TheUnreal  · 技术社区  · 6 年前

    我有一个列表视图,其中充满了作为聊天信息的FireBase集合。

    当用户长按一个ListTile(列表中的一个项目)时,我会显示一个底页。

    当用户单击 Delete 按钮,文件应该被删除。单击“删除”按钮后,无法找到删除所选文档的方法。

    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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Jerome Escalante    6 年前

    要删除文档,我们可以使用 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 文件:

    enter image description here

    删除后 DOC1 :

    enter image description here