【发布时间】:2019-05-28 10:19:25
【问题描述】:
我有一个 ListView,里面装满了 Firebase 集合作为聊天消息。
当用户长按 ListTile(列表中的一个项目)时,我会显示一个 BottomSheet。
当用户单击 BottomSheet 上的Delete 按钮时,文档应该被删除。点击“删除”按钮后,我无法找到删除所选文档的方法。
列表视图小部件:
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
【问题讨论】:
-
尝试去掉onTap上的粗箭头
=>。 -
与其他语言相比,您不能在 dart 中将箭头与 {} 结合使用,您应该尝试类似 onTap:() => snapshot.data.documents[index]["id"]
-
我更新了代码,我仍然无法删除文档,我收到
Too many positional arguments错误。不知道出了什么问题。