'How to delete firebase document on click? [duplicate]

The application displays news cards on which there is an icon that adds news to Favorites.The added news is added to the Firestore database and displayed on a separate page with a delete icon. I need that when clicking on the icon, the document with this news is deleted from the database. How can I do that? Icon code with addition:

Widget customListTile(Article article, BuildContext context) {
  final _fireStore = FirebaseFirestore.instance;
  
  ...
  
   IconButton(onPressed: () async {
                          newsController.addNews(article);
                          _fireStore.collection('favoriteItems').add({
                            'name' : article.source.name,
                            'title': article.title,
                            'image': article.urlToImage,
                          });
                        },
                        icon: const Icon(Icons.bookmark_border)),
}

Icon code with removal:

IconButton(onPressed: () {
                            newsController.removeNews(article);
                          },
                          icon: const Icon(Icons.bookmark_remove))


Solution 1:[1]

To delete a document, we can use the runTransaction method of the Firestore.instance and use the delete method of the Transaction class.

Flutter - remove a firebase document onTap()

await Firestore.instance.runTransaction((Transaction myTransaction) async {
    await myTransaction.delete(snapshot.data.documents[index].reference);
});

Solution 2:[2]

First Get your ID

then, run :

 IconButton(onPressed: () {
      CollectionReference users = FirebaseFirestore.instance.collection('favoriteItems');

      Future<void> deleteItems() {
        return users
            .doc('itemsID')
            .delete()
            .then((value) => print("Items Deleted"))
            .catchError((error) => print("Failed to delete Item: $error"));
      }
    }, icon: const Icon(Icons.bookmark_remove))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vishal_VE
Solution 2 abdul_kayuem