Home > Blockchain >  Firebase: How to delete collection within a collection?
Firebase: How to delete collection within a collection?

Time:10-04

I am developing a cart page in Flutter. For deleting a particular item in the cart, I am facing an issue. I want to delete a document inside collection "items" which is inside collection "myOrders"

myOrders => docID => items => docID(to be deleted)

This is the code I tried,


    Future deleteData(BuildContext context)
    {
      final user = Provider.of<Userr?>(context,listen: false);
      CollectionReference _collectionRef = FirebaseFirestore.instance.collection('myOrders');
      return _collectionRef.doc(user?.uid).collection('items').doc().delete();
    }

[Firebase Sample Image] [1]: https://i.stack.imgur.com/jWCHC.png

I need to know why it is not getting deleted and what change I need to do in the code!

CodePudding user response:

Each time you're calling .doc() to create the following reference, without passing anything as an argument:

 _collectionRef.doc(user?.uid).collection('items').doc()
//                                                               
  • Related