0

I am using this code to upload data to firebase:

List<Map<String, dynamic>> mapData = [
    {
      'name': 'name1',
      'done': false,
      'frequency': '1',
    },
    {
      'name': 'name2',
      'done': false,
      'frequency': '5',
    },
  ];

if (isExist == false && listNameController.text.isNotEmpty) {
        await Firestore.instance
            .collection(widget.user.uid)
            .document(listNameController.text.toString().trim())
            .setData({
          'users': mapData,
        });

enter image description here

But now I want to add more data. I have tried this code but it overwrites all content. What can I do to just add the data to 'users'?

Firestore.instance
.collection(widget.user.uid)
.document(widget.currentList.keys.elementAt(widget.i))
.updateData({
'users': {
'name': 'name3',
'done': false,
'frequency': '7'
},

enter image description here

2
  • Did you try document.setData instead of document.updateData? Commented Nov 7, 2022 at 11:07
  • i have tried that but it also overwrites. Commented Nov 7, 2022 at 11:31

1 Answer 1

1

You can use arrayUnion to add array elements. Please use latest version of firestore package from pub.dev

Firestore.instance
.collection(widget.user.uid)
.document(widget.currentList.keys.elementAt(widget.i)).update({
          "users": FieldValue.arrayUnion([new User]),
 });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.