0

I am trying to make a Flutter and Firebase fitness application that saves runs a user logs. The runs are saved in an array called 'runs' in the Firestore database. Each document in my database represents a different user's runs, so the 'runs' array is a field found within each user's document. I have been trying to delete only a specific child of 'runs' but have had no luck. Does anyone know how to do this?

Here is my code for the delete function:

final CollectionReference runCollection = Firestore.instance.collection('runs')

Future deleteRun(dynamic runToDelete) async {
    return await runCollection.document(uid).setData({
      'runs': FieldValue.arrayRemove([runToDelete])
    });
  }

When I run this, I get no errors but nothing happens in the database.

I have also tried FieldValue.delete() but have not been able to isolate a specific index of 'runs' to delete without deleting the entire array.

Here is a picture of my firestore database: Firebase Database Structure

6
  • Please edit the question to show the exact contents of the document you're trying to change, and the exact value of runToDelete. Without these, we have no idea if you might be doing something wrong. Commented May 12, 2020 at 22:11
  • By the way runCollection.document(uid) will always be non-null. It is not actually querying the database, it is just building a reference to a document that might exist or not. Commented May 12, 2020 at 22:14
  • @DougStevenson runToDelete is of type Map<String, dynamic> and includes data about the run. 'runs' is a list of these maps Commented May 13, 2020 at 3:22
  • Please edit the question to show the exact document and data. Use the edit link at the bottom of the question to make changes. Commented May 13, 2020 at 3:24
  • If possible, add a screenshot from your database as well, so we have a deeper understanding of your structure. Commented May 13, 2020 at 12:58

2 Answers 2

3
Firestore.instance.collection(collection name).document(your document id).updateData({"array name": FieldValue.arrayRemove([delete object/array])}).than((value){print("delete");}).catchError((error){print(error);});
Sign up to request clarification or add additional context in comments.

2 Comments

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
on this answer what worked for me is '''Firestore.instance.collection(collection name).document(your document id).updateData({"array name": FieldValue.arrayRemove(delete object/array)}).then((value){print("delete");}).catchError((error){print(error);});'''
0

FieldValue.delete() only works when you know the entire contents of the array item to delete. It does not work with indexes, nor does it work with child values of array items.

What you will have to do instead is read the document, modify the array in memory, then write the modified array back to the document.

1 Comment

These operations are all well documented. If you have a specific problem, please post a new question with the code that isn't working the way you expect.

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.