Jt Rh
Jt Rh

Reputation: 41

How to update map data in Array? Flutter firebase

I use Flutter. I would like to update the value of CH to be false in Firebase only for the week of 26/4/2020. When is there a way?

Failed Ex.cord:

_firestore.collection('member').document(uidMember[index])
.updateData({'statistics': [{'${submit}': val,}]});

enter image description here

Upvotes: 4

Views: 3625

Answers (2)

CopsOnRoad
CopsOnRoad

Reputation: 268384

First create a List which has the updated items. Create a Map using the list and then update this map to the cloud firestore:

List<Map<String, dynamic>> updatedList = [...];
Map<String, dynamic> updatedData = {
  'statistics': updatedList,
};
  
var collection = FirebaseFirestore.instance.collection('collection');
collection 
    .doc('doc_id')
    .update(updatedData);

Upvotes: 0

Rafael Lemos
Rafael Lemos

Reputation: 5829

Given the Firestore snippet you shared you can do something like the following code to update data:

var ref = _firestore.collection('member').document(uidMember[index]);
ref.get() => then(function(snapshot) {
    List<dynamic> list = List.from(snapshot.data['statistics']);
    //if you need to update all positions of the array do a foreach instead of the next line
    list[0].CH = false;
    ref.updateData({
        'statistics': list
    }).catchError((e) {
        print(e);
    });
}.catchError((e) {
    print(e);
});

NOTE: I haven't tested this so you might have to adapt it, but should be a good starting point.

Upvotes: 3

Related Questions