skip to Main Content

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.

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'});
  }

2

Answers


  1. 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.

    Login or Signup to reply.
  2.   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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search