【发布时间】:2022-01-14 06:14:28
【问题描述】:
不知道如何从 Firestore 中删除文档。
在 PopMenuButton 中,我通过执行以下操作尝试访问 postID 失败:
onSelected: (value) async {
await FirebaseFirestore.instance.collection('thread').doc(FBCloudStore. ).delete();
//await FBCloudStore.deletePostInFirebase(postID);
},
这是我的 FBCloudStore,我认为我需要更改一些东西。????
将 postID 参数移动到局部变量,然后呢?
class FBCloudStore {
static Future<void> sendPostInFirebase(String postID, String postContent,
MyProfileData userProfile, String postImageURL) async {
String postFCMToken;
if (userProfile.myFCMToken == null) {
SharedPreferences prefs = await SharedPreferences.getInstance();
postFCMToken = prefs.get('FCMToken');
} else {
postFCMToken = userProfile.myFCMToken;
}
FirebaseFirestore.instance.collection('thread').doc(postID).set({
'postID': postID,
'userName': userProfile.myName,
'userThumbnail': userProfile.myThumbnail,
'postTimeStamp': DateTime
.now()
.millisecondsSinceEpoch,
'postContent': postContent,
'postImage': postImageURL,
'postLikeCount': 0,
'postCommentCount': 0,
'FCMToken': postFCMToken
});
}
//Should it be like this?????
deletePostInFirebase(String postID) async {
await FirebaseFirestore.instance.collection('thread').doc(postID).delete();
}
}
这是带有 PopupMenuButton 的线程项。
class ThreadItem extends StatefulWidget {
final BuildContext parentContext;
final DocumentSnapshot data;
final MyProfileData myData;
final ValueChanged<MyProfileData> updateMyDataToMain;
final bool isFromThread;
final Function threadItemAction;
final int commentCount;
ThreadItem(
{this.data,
this.myData,
this.updateMyDataToMain,
this.threadItemAction,
this.isFromThread,
this.commentCount,
this.parentContext});
@override
State<StatefulWidget> createState() => _ThreadItem();
}
class _ThreadItem extends State<ThreadItem> {
MyProfileData _currentMyData;
int _likeCount;
@override
void initState() {
_currentMyData = widget.myData;
_likeCount = widget.data['postLikeCount'];
super.initState();
}
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
PopupMenuButton<int>(
itemBuilder: (context) => [
PopupMenuItem(
value: 1,
child: Row(
children: [
Text("Delete post"),
],
),
),
],
initialValue: 1,
onCanceled: () {
print("You have canceled the menu.");
},
onSelected: (value) async {
print('delete');
//TODo how to delete post?
//await FBCloudStore.deletePostInFirebase(postID);
//await FirebaseFirestore.instance.collection('thread').doc(FBCloudStore. ).delete();
},
),
],
);
}
}
【问题讨论】:
标签: flutter google-cloud-firestore