[英]Get ID of current document Flutter / Firebase
I have a list of books.我有一个书单。 User can create reviews about those books.用户可以创建关于这些书的评论。 In a way to make the reviews publics, they need to be verified by staff.为了使评论公开,他们需要由工作人员进行验证。
This is how the reviews are created in Firestore:这是在 Firestore 中创建评论的方式:
Future<void> addChapterReview() {
return chapter_reviews
.add({
'anime_adaptation': animeAdaptation,
'edited_in_france': editedInFrance,
'link': chapterLinkController.text,
'manga': chapterOriginalMangaController.text,
'note': chapterNoteController.text,
'number': chapterNumberController.text,
'opinion': chapterOpinionController.text,
'pic': chapterPicController.text,
'title': chapterTitleController.text,
'edited_in_france': isCheckedEdited,
'anime_adaptation': isCheckedAnime,
'isVerified': false,
})
.then((value) => print('Review ajoutée avec succès.'))
.catchError((error) => print('Echec de l\'ajout de la review.'));
}
After that, admins can check the reviews, validate, modify or delete them.之后,管理员可以检查评论、验证、修改或删除评论。
How I update the review:我如何更新评论:
Future<void> sendValidation() async {
await FirebaseFirestore.instance
.collection("chapters")
.doc("currentDocIdIWant")
.update({'isVerified': 'true'});
}
That where my problem is.那是我的问题所在。 To do any action on reviews, I need to get the current review's id.要对评论执行任何操作,我需要获取当前评论的 ID。 I say 'current' because admin go on each review page to moderate them.我说“当前”是因为管理员会在每个评论页面上进行审核。
In example: I want to moderate a new posted review.例如:我想审核新发布的评论。 I want to get this id:我想得到这个ID:
I can't find a solution that match with my code.我找不到与我的代码匹配的解决方案。
Don't hesitate to ask me mor details if needed.如果需要,请不要犹豫,问我更多细节。
EDIT: OK so with the helpers below and some search, I can't get the id I want.编辑:好的,使用下面的助手和一些搜索,我无法获得我想要的 ID。 So, I put the title in the ID when the review is created (no auto-generated ID).所以,我在创建评论时将标题放在 ID 中(没有自动生成的 ID)。 In this way, I can get and put the title of the current review I'm moderating in my moderation query.通过这种方式,我可以获得我正在审核的当前评论的标题并将其放入我的审核查询中。
Future<void> addChapterReview() {
return chapter_reviews
.doc(chapterTitleController.text)
.set({
'anime_adaptation': animeAdaptation,
'edited_in_france': editedInFrance,
'link': chapterLinkController.text,
'manga': chapterOriginalMangaController.text,
'note': chapterNoteController.text,
'number': chapterNumberController.text,
'opinion': chapterOpinionController.text,
'pic': chapterPicController.text,
'title': chapterTitleController.text,
'edited_in_france': isCheckedEdited,
'anime_adaptation': isCheckedAnime,
'isVerified': 'false',
})
.then((value) => print('Review ajoutée avec succès.'))
.catchError((error) => print('Echec de l\'ajout de la review.'));
}
Future<void> sendValidation() async {
await FirebaseFirestore.instance
.collection("chapters")
.doc(widget.chapterdata['title'])
.update({'isVerified': 'true'});
}
You just need to do this:你只需要这样做:
QuerySnapshot feed =
await FirebaseFirestore.instance.collection("chapters").get();
allDates = [];
for (var element in feed.docs) {
allDates.add(element.id);
}
Doing this you'll get a list containing all the document ids in your chapters collection.这样做你会得到一个列表,其中包含章节集合中的所有文档 ID。 Then you can use whichever you like:)然后你可以使用任何你喜欢的:)
PS I'd recommend organizing your document ids and not to use auto-generated ones as they can add complexity in finding and addition or removal of others. PS 我建议组织您的文档 ID,不要使用自动生成的文档 ID,因为它们会增加查找和添加或删除其他文档的复杂性。
Future<void> addChapterReview() {
final documentAdded =
await chapter_reviews.add({
'anime_adaptation': animeAdaptation,
'edited_in_france': editedInFrance,
'link': chapterLinkController.text,
'manga': chapterOriginalMangaController.text,
'note': chapterNoteController.text,
'number': chapterNumberController.text,
'opinion': chapterOpinionController.text,
'pic': chapterPicController.text,
'title': chapterTitleController.text,
'edited_in_france': isCheckedEdited,
'anime_adaptation': isCheckedAnime,
'isVerified': false,
});
final savedDocumentID = documentAdded.id;
}
You can do whatever you want with that ID你可以用那个ID做任何你想做的事
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.