Home > Mobile >  Get ID of current document Flutter / Firebase
Get ID of current document Flutter / Firebase

Time:12-20

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 :screenshot example

I can't find a solution that match with my code.

Don't hesitate to ask me mor details if needed.

CodePudding user response:

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 :)

P.S. 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.

CodePudding user response:

  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

  • Related