Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 494
Zain Thaver Avatar asked Nov 07 '25 06:11

Zain Thaver


2 Answers

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.

like image 77
Peter Haddad Avatar answered Nov 09 '25 02:11

Peter Haddad


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),
    );
like image 33
Hrvoje Čukman Avatar answered Nov 09 '25 02:11

Hrvoje Čukman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!