userName
userName

Reputation: 955

How to remove fields from a database?

When I click on the icon, the fields of the news that was clicked are added to the database:

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)),

How now, when clicking on another icon, you can delete these values ​​from the database?

 IconButton(onPressed: () {
                            
                          },
                          icon: const Icon(Icons.bookmark_remove))

Upvotes: 0

Views: 53

Answers (1)

abdul_kayuem
abdul_kayuem

Reputation: 49

First, get your ID

Then, Write this Lines:

 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))

Upvotes: 1

Related Questions