-1

i have created an array of strings "challengeMember" in Firestore database and i want to add data to it when user clicks a button, my database:

enter image description here

in my code am trying to update the array, every time user join a challenge, the challenge name will be added to this array i know how to access the array using:

FirebaseFirestore.instance.collection("Users").doc(user!.uid).update({'challengeMember': widget._challengeName});

but the problem with this it is not specifying the array index and it is not checking whether the index is empty or not, so how can i dynamically access this array and keep adding values.

1 Answer 1

2

Firestore has a special trick to add an array, and it goes like this:

await FirebaseFirestore.instance.collection('users').doc(uid).update({
        'tokens': FieldValue.arrayUnion([token]),
      });

See the FieldValue documentation on how to manipulate arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for help i really appreciate that now i understand how to manipulate arrays in Firestore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.