skip to Main Content

So, here is what I am trying to do. I am clicking on a button (from a popup alert) to delete a particular firebase record then navigating back until route.isFirst and then one more step back because I don’t want to stay on a page that is deleted in database.
Here is my code (not working)

TextButton(
              child: Text(
                "Delete",
                style: TextStyle(fontWeight: FontWeight.bold),
              ),
              onPressed: () async {
                await FirebaseFirestore.instance
                    .collection("board")
                    .doc(selectedDocumentId)
                    .collection(selectedDocumentId)
                    .get()
                    .then((snapshot) {
                      for (DocumentSnapshot doc in snapshot.docs) {
                        doc.reference.delete();
                      }
                    })
                    .then((value) async {
                      await FirebaseFirestore.instance
                          .collection("board")
                          .doc(selectedDocumentId)
                          .delete();
                    })
                    .**then((value) =>
                        Navigator.popUntil(context, (route) => route.isFirst))
                    .then((value) =>
                        Navigator.of(context, rootNavigator: true).pop());
              }),**

2

Answers


  1. Since the delete is awaited the next call to navigator can be executed after that without adding a then. Also if you navigate to the first route you must have already reached the first page of the app where there is no more page to pop.

    onPressed: () async {
                    await FirebaseFirestore.instance
                        .collection("board")
                        .doc(selectedDocumentId)
                        .collection(selectedDocumentId)
                        .get()
                        .then((snapshot) {
                          for (DocumentSnapshot doc in snapshot.docs) {
                            doc.reference.delete();
                          }
                        })
                        .then((value) async {
                          await FirebaseFirestore.instance
                              .collection("board")
                              .doc(selectedDocumentId)
                              .delete();
     Navigator.popUntil(context, (route) => route.isFirst));
    })
                  }),
    
    Login or Signup to reply.
  2. Use this code to return to a specific page, deleting all pages in the stack:

    Navigator.of(context).pushAndRemoveUntil(
      MaterialPageRoute(builder: (context) => name_of_home_page()),
      (Route<dynamic> route) => false,
    );
    

    If you want to restart the application instead (because maybe you have changed some parameters or something else), you can use Phoenix (https://pub.dev/packages/flutter_phoenix)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search