0

FireStore structure

so the menus field stores maps inside of an array/List. But when i want to add a map to menus it replaces all the existing ones with the new one, instead of adding a new one, here my code:

    Menu menuObj = Menu(
      name: menuName, categories: [
      Categories(
        name: categoryName,
      ),
    ]);

FirebaseFirestore.instance.doc('menus/${FirebaseAuth.instance.currentUser!.uid}')
        .set({
      'menus': [
        menuObj.toJson(),
      ]
    }, SetOptions(merge: true));
2
  • have you tried changing your set method to update on firestore instance I guess that should work Commented Oct 9, 2021 at 9:08
  • @AbhishekVishwakarma nope that didn't work as well but I found out and wrote an answer below stackoverflow.com/a/69501959/14924556 Commented Oct 9, 2021 at 17:00

1 Answer 1

3

I had to use this code instead:

    FirebaseFirestore.instance
        .doc('menus/${FirebaseAuth.instance.currentUser!.uid}')
        .set({
      'menus': FieldValue.arrayUnion([menuObj.toJson()])
    }, SetOptions(merge: true));

Important part is this: FieldValue.arrayUnion([menuObj.toJson()]).

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.