'Flutter: Update specific index in list (Firestore)

How exactly do you update a specific value in a list based on it's index? For example, in the following list:

0 [
    {
      first_name: name0,
      last_name: lastName0,
    }
  ]
1 [
    {
      first_name: name1,
      last_name: lastName1,
    }
  ]          

How can I update only "lastName1"? At the moment, I'm using a bit of a hacky solution using ArrayUnion, which uses data comparison instead of the actual index (I load the values into a form, delete the original value and then re-add it again):

 // Retrieve data at index, load into form

// Remove that index from List 
await Firestore.instance.collection(col).document(doc).updateData({
  'people': FieldValue.arrayRemove([person])
});

// Make changes to data via form

// Save back to list
 await Firestore.instance.collection(col).document(doc).updateData({
  'people': FieldValue.arrayUnion([person])
});

Is there a function that will allow me to specify which specific index's data to update? Something like (but using a List, not Map):

 await Firestore.instance.collection(col).document(doc).updateData({
  'people.$index.lastName1': 'newName')
 });


Solution 1:[1]

For all client languages and platforms, it's currently not possibly to issue a single command to update an array item by index. There is no special syntax for that. What you will need to do instead is read the document, modify the field array data in the way that you want, and update that field back to the document, in its entirety. You can do this in a transaction if you want the update to be atomic.

See also:

Solution 2:[2]

You can mention the index value after the field name, it will work properly,

await FirebaseFirestore.instance
                      .collection('collectionPath')
                      .doc(uid)
                      .update({
                    "Class Details"[0]: FieldValue.arrayUnion([
                      {
                        "Price": 3000,
                        "Paid": true,
                      }
                    ])
                  });

I mentioned that on my fieldname like "Class Details"[0] it will work, Thanks for reading , Have a good day,

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 Doug Stevenson
Solution 2 Jabaseelan S