简体   繁体   English

获取当前文档的 ID Flutter / Firebase

[英]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.

相关问题 如何按日期对 Firestore 文档 ID 进行排序 firebase flutter - How to sort firestore document id date wise firebase flutter 如何获取 firebase Firestore 文档 ID - How to get the firebase firestore document id Firestore - 获取路径未知的 ID 文档 - Firebase Cloud Functions - Firestore - Get document with ID whose path is unknown - Firebase Cloud Functions 当前用户 ID 在退出后保持不变 | Firebase 认证 | Flutter Web - Current user id is persistent after signing out | Firebase Auth | Flutter Web 获取 Firebase 文档快照到 Map - Get Firebase Document Snapshot to a Map 如何在 Firebase 中为 Flutter 检查 null 当前用户 - How to check for null current user in Firebase for Flutter 在 Flutter 的嵌套文档内的嵌套数组内从 Firebase 实时数据库获取结果流 - Get stream of results from Firebase Realtime Database inside nested array inside nested document in Flutter Flutter firebase,使用一个文档中的引用调用另一个文档 - Flutter firebase, use a reference in a document to call another document Flutter &amp; firebase 通过 geohash 获取文档 - Flutter & firebase get docs by geohash Firebase:使用缩短版本的文档 ID 是否可靠? - Firebase: Is it reliable to use a shortened version of a document ID?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM