Zain Thaver
Zain Thaver

Reputation: 61

Adding Data into A map field (Flutter Firestore)

I am using cloud_firestore for flutter .I want to add onto a map field (userInfo) of a specific document of my firebase database on the press of a button. I do not want to add any other fields, only append more data to the map(userInfo).In this case using this code name is always unique (the userID) of a user.

    Firestore.instance.collection("prayerRooms")
              .document(docID)
              .updateData({

              'userInfo.userId': name,
              'userInfo.userCount': 2


          }
          );

Here is the database in question

Upvotes: 4

Views: 5558

Answers (3)

iAugustozSatoshi
iAugustozSatoshi

Reputation: 66

2023:

 FirebaseFirestore db = FirebaseFirestore.instance;
 db.collection("collection")
   .doc("DOC-ID")
   .update({
     'userInfo.users': "user1"
   });

Upvotes: 0

Hrvoje Čukman
Hrvoje Čukman

Reputation: 447

You can user SetOptions(merge: true) for that

Full code:

Firestore.instance.collection("prayerRooms")
      .document(docID)
      .set({
        'userInfo.userId': name,
        'userInfo.userCount': 2
      },
      SetOptions(merge: true),
    );

Upvotes: 1

Peter Haddad
Peter Haddad

Reputation: 80904

This code:

 Firestore.instance.collection("prayerRooms")
              .document(docID)
              .updateData({
              'userInfo.userId': name,
              'userInfo.userCount': 2
          });

will update both the userId and the userCount inside the userInfo map. If you want to add more attribute inside the userInfo map, then you can do:

 Firestore.instance.collection("prayerRooms")
              .document(docID)
              .updateData({
              'userInfo.users': "user1",
          });

This will add a new attribute inside the map called users.

Upvotes: 2

Related Questions