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?

5
  • stackoverflow.com/questions/74345498/… Commented Nov 7, 2022 at 11:56
  • Can you try this ? Hope it helps :) Commented Nov 7, 2022 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. Commented Nov 7, 2022 at 12:36
  • 2
    Get the array from the database first. Update the array locally and then push the whole array back. Commented Nov 7, 2022 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. Commented Nov 7, 2022 at 14:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.