2

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);
      }
    },
  ),
)),
2
  • Please edit your question and add your database structure as a screenshot and indicate the exact data you want to delete. Commented Feb 8, 2022 at 11:58
  • I have just edited my question Commented Feb 8, 2022 at 12:09

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

but what I do If a have more adresses and I want to delete just one?
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.
Ups! I update the photo to show exacthy what I want, sorry for the mistake
In that case, please check my updated answer.
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?
|

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.