Delete firestore document using Slidable

Viewed 100

I'm trying to implement a Slidable widget to delete a chosen document from a firestore snapshot. Here's my code:

return Expanded(
    child: ListView(
      children: snapshot.data!.docs.map((DocumentSnapshot document) {
        Map<String, dynamic> data =
            document.data()! as Map<String, dynamic>;
        return Card(
          color: const Color(0xff303437),
          elevation: 0,
          margin: const EdgeInsets.only(
            top: 10,
            bottom: 10,
            right: 0,
            left: 0,
          ),
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(20),
            ),
          ),
          child: Padding(
            padding: const EdgeInsets.only(left: 10, right: 10),
            child: Slidable(
              endActionPane: ActionPane(
                motion: const ScrollMotion(),
                children: [
                  SlidableAction(
                    key: ObjectKey(data['date']),
                    onPressed: deleteData,
                    backgroundColor: const Color(0xfffe4A49),
                    foregroundColor: Colors.white,
                    icon: Icons.delete,
                    label: 'Delete',
                  ),
                ],
              ),
              child: ListTile(
                ..
              )

And for now the function deleteData only does this:

void deleteData(BuildContext context) {
  debugPrint(context.widget.key.toString());
}

What I'm trying to accomplish is: use the document 'date' as key to delete it, but the debugPrint give me null every time. I'm sure data['date'] works because I can see each of them in the ListTile.

How can I pass the key to the deleteData function,so I can filter all the documents by timestamp and delete the correct one?

1 Answers

I resolved by using dismissible:

child: Slidable(
  key: ValueKey(data['date']),
  endActionPane: ActionPane(
    extentRatio: 0.3,
    motion: const ScrollMotion(),
    dismissible: DismissiblePane(
      onDismissed: () {
        debugPrint(data['date'].toString());
        FirebaseFirestore.instance
          .collection('users/$uid/receipts')
          .where('date', isEqualTo: data['date'])
          .limit(1)
          .get()
          .then((value) {
            value.docs.forEach((element) {
              FirebaseFirestore.instance
                .collection('users/$uid/receipts')
                .doc(element.id)
                .delete();
              });
            });
          },
        ),
        children: const [
          SlidableAction(
            onPressed: doNothing,
            backgroundColor: Color(0xfffe4a49),
            foregroundColor: Colors.white,
            icon: Icons.delete,
            label: 'Delete',
          ),
        ],
      ),
      child: ListTile(
        ..
      ),
    ),

And for now the onPressed action will do nothing. I'll provide a complete implementation when I'll find it, for now this is good enough.

Related