8

I'm having a problem right now in firebase. Where I try to delete/remove a specific array data. What is the best way to do it? Ps. I'm just new in firebase/flutter.

My database structure:

enter image description here

Data that i'm trying to remove in my database structure(Highlighted one):

enter image description here

5 Answers 5

16

First create a blank list and add element in the list which you want to remove then Update using below method

Note : For this method you need the documennt id of element you want to delete

var val=[];   //blank list for add elements which you want to delete
val.add('$addDeletedElements');
 Firestore.instance.collection("INTERESTED").document('documentID').updateData({

                                        "Interested Request":FieldValue.arrayRemove(val) })
Sign up to request clarification or add additional context in comments.

2 Comments

Similarly, how to do opposite of this? That is, how to add a single element to an existing array in firebase? Thanks
@KrishnaShetty use FieldValue.arrayUnion(val)
6

Update: Much has changed in the API, although the concept is the same.

var collection = FirebaseFirestore.instance.collection('collection');
collection 
  .doc('document_id')
  .update(
  {
    'your_field': FieldValue.arrayRemove(elementsToDelete),
  }
);

Comments

4

Firestore does not provide a direct way to delete an array item by index. What you will have to do in this case is read the document, modify the array in memory in the client, then update the new contents of the field back to the document. You can do this in a transaction if you want to make the update atomic.

1 Comment

this needs to be added to the documentation.
2

This will help you to add and remove specific array data in could_firestore.

getPickUpEquipment(EquipmentEntity equipment) async{


final equipmentCollection = fireStore.collection("equipments").doc(equipment.equipmentId);

final docSnap=await equipmentCollection.get();

List queue=docSnap.get('queue');

if (queue.contains(equipment.uid)==true){

  equipmentCollection.update({

    "queue":FieldValue.arrayRemove([equipment.uid])

  });

}else{


  equipmentCollection.update({

    "queue":FieldValue.arrayUnion([equipment.uid])

  });

}

}

Example

ezgif com-crop(1)

Comments

0

I had also experienced this issue and below is how I solved it. My case description I wanted to remove cart item from a user collection . Below is how User my collection looks like enter image description here

Future removeItemFromCart(OrderModel order) async {

try {
  var userId = _firebaseAuth.currentUser?.uid;
  final collection = FirebaseFirestore.instance
      .collection(FirestoreConstants.pathUserCollection)
      .doc(userId);
  final docSnap = await collection.get();
  List cart = docSnap.get('cart');
  List item = cart
      .where((element) => element['orderId'].contains(order.orderId))
      .toList();
  collection.update({'cart': FieldValue.arrayRemove(item)});

  Fimber.d("REMOVED unit ${order.unit?.unitName} $userId");
  PaceUser? user = await getUserCart();
  return user;
} on FirebaseAuthException {
  rethrow;
}
return null;}

The above code snippet solved it for me.

Happy coding

Comments

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.