1

How can I delete an array of array of objects from Firestore? I want to delete it inside a list.generate

Here is my database structure, I want to delete [{street A, number 25}]

enter image description here

List.generate(
  ...
  IconButton(
    icon: const Icon(CupertinoIcons.trash),
    onPressed: () async {
      try {
        await FirebaseFirestore.instance
            .collection('users')
            .doc(data['uid'])
            .update(
          {
            'adress':
                FieldValue.arrayRemove(
                    ??)
          },
        );
      } catch (e) {
        print(e);
      }
    },
  ),
)),
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mus
  • 67
  • 1
  • 13

1 Answers1

3

As I see in your screenshot, the adress field is not of type array, it's a Map:

enter image description here

So there is no way you can call FieldValue.arrayRemove. If you want to remove that field, then please use the following lines of code:

Firestore.instance.collection('users').document(data['uid']).set(
    {'adress': FieldValue.delete()},
    SetOptions(
      merge: true,
    ),
)

Edit:

After you have updated the screenshot, now the adress field is indeed of type array:

enter image description here

If you want to delete a specific object from the array using FieldValue.delete(), you should use all the data in the object and not partial data. I have even written an article regarding updating an array of objects in Cloud Firestore:

Edit2:

In code it should look like this:

Firestore.instance.collection('users').document(data['uid']).update({
    'adress': FieldValue.arrayRemove(elementToDelete),
});

Please note that elementToDelete object should contain both fields populated. The number should hold the value of "25" and street should hold the value of "street A". It will not work if you use only one.

However, if you thought that Firestore provides a direct way to delete an array element by index, please note that is not possible. In such a case, you'll have to read the document, remove the element from the array, then write the document back to Firestore.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • but what I do If a have more adresses and I want to delete just one? – Mus Feb 08 '22 at 12:28
  • There is no way you can add multiple addresses under the `adress` field. Since it's a Map, it doesn't allow duplicate keys. So only one address can be stored. If you want to add multiple addresses then you should change the type of the property from Map to array. – Alex Mamo Feb 08 '22 at 12:31
  • Ups! I update the photo to show exacthy what I want, sorry for the mistake – Mus Feb 08 '22 at 12:44
  • In that case, please check my updated answer. – Alex Mamo Feb 08 '22 at 13:23
  • I can't follow your tutorial, I'm new with flutter and I I am not able to fully understand it. Can u please wrote a simple solution for me? – Mus Feb 08 '22 at 14:09
  • Thanks it works for me, every field must be same, so that can be executed. – rooxane.mac Mar 25 '23 at 17:06