0

I am developing a Quiz App using flutter, firebase and cloud firestore in which users can play a quiz only once per day and the total score of all users will be reset to zero after every month. I am trying to achieve this by creating these two fields in each user document in the users collection:

  • isReadyToPlay: This field stores a boolean value 'true' if user didn't play the quiz, and changes to 'false' when user completes the quiz. I display a "start quiz" button if the value is true and remove it if the value is false. But I need to update this value to 'true' again, to allow the user to play the quiz the next day. So how do I update this field in all the documents from the collection?
  • total_score: This field stores the total score of all the quizzes a user plays. I want to reset this score to zero after every month. So again, I need to update this field in all the documents of the users collection.

I want to update the mentioned fields in all the documents at once from the admin end. If this is not possible, what could be a work around to achieve this?

Prudhvi
  • 116
  • 3
  • 15

1 Answers1

2

You can do it like this first get all the documents as a snapshot and just update the required field of each document in the snapshot.

 FirebaseFirestore.instance.collection('users').get().then((snapshot) {
            for (DocumentSnapshot ds in snapshot.docs) {
              ds.reference.update({
                'isReadyToPlay': true, //True or false
                'totalScore': 254 //Your new value
              });
            }
          })
Kaleb
  • 541
  • 2
  • 7