简体   繁体   中英

FlutterFire update Array instead of replacing it

FireStore 结构

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));

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()]) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM