Search code examples
flutterfirebasegoogle-cloud-firestore

Add multiple maps to a single firestore document with unique map 'field' identifiers


I have a dropdown in my flutter app. which contains several different items which users can add or remove in the app. But this happens rarely since mostly a user will choose one of these items from dropdown as it is and proceed. So I created a document and added above mentioned dropdown fields as seperate maps.

firebase console view of the document

Now my question is when user trying to add a new field to a dropdown what should be the map field value. If I accidently use a field name of a previous map it will replace the previous map and I don't want that to happen. Also as in SQL world firestore dosen't support automatically incrimenting fields that option dosen't work for me either.


Solution

  • I'm not sure I understand correctly, but I think that you may be looking for an array field.

    If you use a single array field to represent the items int the dropdown list, you can use the arrayUnion operator to add values to that array field, and you'll no longer need to assign unique, auto-incrementing index values yourself.