简体   繁体   中英

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:

  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. 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: 截图示例

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. So, I put the title in the ID when the review is created (no auto-generated 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. 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.

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM