Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a field in Firestore document with flutter

I'm making a Flutter application.

But, I cannot delete a field in the Firestore document. In another language I know to use FieldValue.delete() to delete a file in Firestorm document.

In Dart, How do I delete?

like image 732
sekitaka Avatar asked Aug 01 '18 12:08

sekitaka


5 Answers

Get the DocumentReference, invoke update with a map which has a key you want to delete and value FieldValue.delete().

var collection = FirebaseFirestore.instance.collection('collection');
collection
  .doc('document_id')
  .update({
    'field_to_delete': FieldValue.delete(),
    }
);
like image 139
CopsOnRoad Avatar answered Oct 20 '22 23:10

CopsOnRoad


Update Oct,2018: This is Now Possible:

In order to delete a particular field from a Cloud Firestore document - make sure you are using Plugin version 0.8.0 or Above. Now a E.g If you have a document having a field 'Desc' with contain some Text. In Order to Delete it.

Firestore.instance.collection('path').document('name').update({'Desc': FieldValue.delete()}).whenComplete((){
  print('Field Deleted');
});

This will Delete 'Desc' Field from the Document 'name'

like image 23
anmol.majhail Avatar answered Oct 24 '22 08:10

anmol.majhail


I think this is currently impossible in standard, non hacky way. There is an open issue https://github.com/flutter/flutter/issues/13905 in Flutter which have to be resolved first.

like image 3
antygravity Avatar answered Oct 24 '22 09:10

antygravity


Here is an official solution for deleting Field Path when programming in Flutter/Dart

Here is how a sample Firestore collection looks like:

{
   'path': {
      'name': { 
        'title': 'my title'
        'description': 'my description',
       }
   }
}

In order to delete description field, you can do this:

Firestore.instance.collection('path').document('name').set(
    {'description': FieldValue.delete()},
    SetOptions(
      merge: true,
    ),
)

And the collection will look like this:

{
   'path': {
      'name': { 
        'title': 'my title'
       }
   }
}
like image 2
lcsvcn Avatar answered Oct 24 '22 09:10

lcsvcn


if you have nested fields then use the '.' (Dot) notation to specify the field.

E.g if your data is nested Map then this is handy.

Firestore.instance.collection('path').document('name').update({'address.town': FieldValue.delete()}).whenComplete((){
  print('Field Deleted');
});
like image 1
Mathulan Avatar answered Oct 24 '22 08:10

Mathulan