简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM