'Flutter Firebase firestore append data with unique ID

I'm working on the Flutter app where users can save multiple addresses. Previously I used a real-time database and it was easier for me to push data in any child with a unique Id but for some reason, I changed to Firestore and the same thing want to achieve with firestore. So, I generated UUID to create unique ID to append to user_address

This is how I want

enter image description here

and user_address looks like this

enter image description here

And this is how it's getting saved in firestore

enter image description here

So my question Is how I append data with unique id do I have to create a collection inside users field or the above is possible?

Below is my code I tried to set and update even user FieldValue.arrayUnion(userServiceAddress) but not getting the desired result

var uuid = Uuid();
var fireStoreUserRef =
    await FirebaseFirestore.instance.collection('users').doc(id);

Map locationMap = {
  'latitude': myPosition.latitude,
  'longitude': myPosition.longitude,
};

var userServiceAddress = <String, dynamic>{
  uuid.v4(): {
    'complete_address': completedAddressController.text,
    'floor_option': floorController.text,
    'how_to_reach': howtoreachController.text,
    'location_type': locationTag,
    'saved_date': DateTime.now().toString(),
    'user_geo_location': locationMap,
    'placeId': addressId
  }
};

await fireStoreUserRef.update({'user_address':  userServiceAddress});

If I use set and update then whole data is replaced with new value it's not appending, so creating a collection is the only solution here and If I create a collection then is there any issue I'll face?



Solution 1:[1]

Got it! I found that responding to Closing could be stopped for study with a break point. Then poked around in the Properties, and tried a few things. Then ultimately googled and found the answer at Do not close ContextMenuStrip on selection of certain items. I added AppFocusChange as not a reason to close.

So far, I'm now able to click the members and select them. Progress!

private void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
    {
         if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked || 
            e.CloseReason == ToolStripDropDownCloseReason.AppFocusChange)
                e.Cancel = true;
    }
     if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
        {
            for (int i = 0; i < 4; i++)
            {
                if (contextMenuStrip1.Items[i].Selected == true)
                {
                    bool res = DoChoice(i);
                    if (res == true) e.Cancel = false;


                }
            }
        }

Then, at DoChoice, I'll invoke the Add or Edit accordingly, and always returna Close to the strip.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1