How To Add Data To The Array Of Hash Map In The Document Using Firestore?
I want to add data to the existing array without overwriting. And inside each document, I have an array of HashMap. I am trying to add data to the existing one. Kindly check the b
Solution 1:
As I see in your screenshot, in your patients
collection you have documents that contain an array that holds maps (objects). In order to be able to update those maps that exist within your array you should create a Map
object that corresponde to your exact document structure and use DocumentReference's set(Object data, SetOptions options) method for that. So pass as the first argument the map and as the second argument SetOptions.merge()
. You can find a simpler example in my answer from the following post:
Solution 2:
This is one of my example
i have done it like this
List<Map> history = List(); HistoryhistoryObj= History(typee, name, timeStamp, pointsBefore, points, itemPoints); history.add(historyObj.toMap()); //Firesore collection object CollectionReferencechild= _firestore.collection('children'); //Firesore document object DocumentReferencedocRef= child.doc(selectedChild.childId); // "history" is your "patient_case" docRef.update({"history": FieldValue.arrayUnion(history)}).then( (value) => print("Update done"));
History is one of my class
History class includes method toMap() which converts all history class variables and its values to 'Key' : 'value' form like this
Map<String, dynamic> toMap() => { "type": type, "name": name, "timestamp": timestamp, "points_before": points_before, "points_after": points_after, "points_affected": points_affected, };
Post a Comment for "How To Add Data To The Array Of Hash Map In The Document Using Firestore?"