Reputation: 117
I have a snapshot of an array that contains imageUrl here. But I want to delete it all at once when I press the button. How can I do that is it possible?
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('widgets')
.doc(widgets)
.snapshots(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
//Array of snapshot imageUrl
var imageUrl = snapshot.data?['imageUrl'];
if (snapshot.data == null) {
return Container();
}
return body: ...
ElevatedButton(
onPressed: () => {openDialog(imageUrl)},
//Dialog to delete the imageUrl
Future openDialog1(imageUrl) => showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Center(child: Text('Delete image')),
content: const Text(
'Warning.\n Once you delete this. You can not bring this back.'),
actions: [
OutlinedButton(
onPressed: () async => {
await storage.ref('images/$imageUrl').delete(),
},
child: const Text(
'Confirm',
style: TextStyle(color: Colors.grey, fontSize: 16.0),
),
)
],
));
images:
ImageUrl in my Firebase Firestore
Image that I want to delete in the FB storage
Upvotes: 0
Views: 31
Reputation: 4556
To delete a file, first create a reference to that file. Then call the delete()
method on that reference.
// Create a reference to the file to delete
final desertRef = storageRef.child("images/desert.jpg");
// Delete the file
await desertRef.delete();
Source: https://firebase.google.com/docs/storage/flutter/delete-files#delete_a_file
Upvotes: 1