0

I am trying to develop a stock management application on Flutter. I have integrated Firebase into the app and I can view the data on the app. My collection and documents are as follows: enter image description here

I want to add a new element to an array in an already existing document wtih this.

Map<String, dynamic> urunData = {
                      'ad': _adController.text,
                      'kod': _barcodeController.text,
                      'seri': _seriController.text,
                      'raflar': [
                        {'raf': dropdownValue, 'adet': _rafController.text},
                      ],
                      'sirket': _sirketController.text,
                      'tarih': FieldValue.serverTimestamp()
                    };

                    sayimRef.doc(_barcodeController.text).update(urunData);

But in this way, the other elements in the 'raflar' array inside document are deleted and it is transformed into a new array with one element. How do I update an existing index or add a new one without deleting other elements?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kerem Yavuz
  • 37
  • 1
  • 8
  • https://stackoverflow.com/questions/74345498/flutter-how-to-add-map-to-existing-array-on-firebase – Rohit Nov 07 '22 at 11:56
  • Can you try this ? Hope it helps :) – Rohit Nov 07 '22 at 11:56
  • Can't say it's fully working. :( It helps me to add a new array item, but this time I can't update the existing value. For example in `raflar` array I have `raf-2c` in index 0. But it creates new `raf-2c` in index 2. – Kerem Yavuz Nov 07 '22 at 12:36
  • 2
    Get the array from the database first. Update the array locally and then push the whole array back. – Afridi Kayal Nov 07 '22 at 14:01
  • There is indeed no way to **update** an item at an index in an array. You'll need to read the array from the document, update it in your application code, and write the entire array back to the database again. – Frank van Puffelen Nov 07 '22 at 14:30

0 Answers0