'flutter onClick delete image from firebase storage and firestore

I need on click delete image from firebase storage and firestore I've written this code that does the delete from firestore only.

onLongPress: ()async{
  await FirebaseFirestore.instance.runTransaction((Transaction myTransaction) async {
    await myTransaction.delete(snapshot.data!.docs[index].reference);
  });
},

screen from code

enter image description here



Solution 1:[1]

You can use FirebaseStorage.instance.refFromURL(url).delete()

like

onLongPress: ()async{
  await FirebaseFirestore.instance.runTransaction((Transaction 
 myTransaction) async {
    FirebaseStorage.instance.refFromURL(snapshot.data!.docs[index].get('url')).delete()
    await myTransaction.delete(snapshot.data!.docs[index].reference);
  });
},

Solution 2:[2]

 child: ListView.builder(
                          itemCount: snapshot.data?.docs.length,
                          itemBuilder: (context, index) {
                            DocumentSnapshot messages = snapshot
                                .data?.docs[index] as DocumentSnapshot<Object?>;
                            return ListTile(
                              subtitle: Text('From: ${messages['sender']}'),
                              title: Text('Text: ${messages['text']}'),
                              tileColor:
                                  selectedIndex == index ? Colors.blue : null,
                              onTap: () async {
                                setState(() {
                                  selectedIndex = index;
                                });

                                await FirebaseFirestore.instance.runTransaction(
                                    (Transaction myTransaction) async {
                                  await myTransaction.delete(snapshot
                                      .data!.docs[selectedIndex!].reference);
                                });
                              },
                            );
                          }));

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 Kishan Busa
Solution 2 Mohammed Salim Al-Othman