Zaeem
Zaeem

Reputation: 235

Can't delete field from a firestore document (Flutter)

I'm trying to delete a field from firestore document in flutter, but it's not working. I'm using the same path to store and retrieve files from firebase storage, and the delete() function works perfectly with firebase storage, but not with the fields of firestore documents. Here's the code:

Firestore.instance.collection("Albums").document("$userName")
    .updateData({'$fileName': FieldValue.delete()}).whenComplete(() => print("Deleted"));

It prints 'Deleted,' but does not delete the specified field from the firestore document.

Upvotes: 1

Views: 849

Answers (2)

MaryM
MaryM

Reputation: 172

As Doug mentioned it seems you are passing the literal strings. You can refer to the official Firestore documentation for guidance. There are more examples here

var cityRef = db.collection('cities').doc('BJ');

// Remove the 'capital' field from the document
var removeCapital = cityRef.update({
    capital: firebase.firestore.FieldValue.delete()
});

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317352

You are passing the literal strings "$userName" and "$fileName" to Firestore, not the value of the variables userName and fileName. If you want to use the value of variables do it like this:

var data = {};
data[fileName] = FieldValue.delete();
Firestore.instance.collection("Albums").document(userName)
    .updateData(data)

Upvotes: 2

Related Questions