5

I've been trying to create a demo "Schedule-App" powered by Google's Firebase, but I'm facing some difficulties updating specific values of an Array with Map values on the side of Firestore.

As you can see on the following image, what we want to achieve is onButtonPress, this specific value to be updated.

enter image description here

I've created a Stream of data and manage it via StreamProvider.

StreamProvider<DocumentSnapshot>.value( value: FirebaseFirestore.instance.collection('schedule').doc(FirebaseAuth.instance.currentUser.uid).snapshots(), 

I'm using the following method to update Array's data, but it doesn't work.

 Future<void> updateUserData(int index, String title, String text, int hour, bool check) async { DocumentReference snapshots = FirebaseFirestore.instance.collection('schedule').doc(FirebaseAuth.instance.currentUser.uid); snapshots.update({ ['standard_schedule'][index]: {'title': title, 'text': text, 'hour': hour, 'check': check} }); } 

What changes should I make to the method above, so that the bool value to be updated, every time the button is pressed?

===============================================

SOLUTION (10/24/2020): Firestore does not provide a way to update an array element at an index. You have to read the entire document, modify the array in memory, then write the entire array back to the document.

3
  • 1
    Firestore does not provide a way to update an array element at an index. You have to read the entire document, modify the array in memory, then write the entire array back to the document. Commented Oct 23, 2020 at 21:40
  • As it seems, this still is the case. All the related answers and articles I've read were at least 6 months old and I wasn't sure if this is still the case. Anyway, thank you for your reply. Commented Oct 24, 2020 at 16:01
  • Hi I've come across issue I'm trying to slove it but I've had stucked ,I'm using arrayRemove n ArrayUnion but not working , Commented Aug 8, 2022 at 8:03

2 Answers 2

3

Firestore does not provide a way to update an array element at an index. You have to read the entire document, modify the array in memory, then write the entire array back to the document.

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

Comments

0

This is my solution.

var array=[{'title': title, 'text': text, 'hour': hour, 'check': check}]; snapshots.update({'standard_schedule': FieldValue.arrayUnion(scores)}); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.