Home > Enterprise >  How to create UNDO button, flutter firebase
How to create UNDO button, flutter firebase

Time:12-08

In my project I have a delete button that deletes some data from firebase firestore, what i'm trying to do is showing a snackbar with undo word, when it's pressed the process will be cancelled and no data will be deleted. I haven't found a solution yet. Is it possible?

CodePudding user response:

Deletes in Firestore are permanent; once you delete a document it is gone.

If you want to give the option to undo the deletion, you'll want to:

  • Either create the document in another collection before deleting it from the main one. For example, a common implementation is to have a subcollection with deleted documents, or even the entire history of each document.
  • Or you mark the documents as deleted, instead of really deleting them. This type of marking is typically referred to as a tombstone.

CodePudding user response:

You can use a Timer to create a callback that can be canceled anytime before the timer runs out.

import 'dart:async';

void main() {
  
  final cancelable = Timer(Duration(seconds: 5), () {
      // The delete function should be here
      print('canceled after 3 seconds');
    });
    
  Timer(Duration(seconds: 3), cancelable.cancel);
  
}

In this example the print() callback gets canceled after three seconds, but you could pass the cancelable.cancel() function as a callback for the undo button and cancel the deletion on Firestore by clicking it before the Timer runs out

  • Related