skip to Main Content

My page have some api request, when api return error, I will show the dialog.

When I click the confirm button, the dialog should be dismiss and current page will finish.

var count = 0;
Navigator.popUntil(context, (route) {
  return count++ == 2;
});

but sometimes it works sometimes it not work.(When I reopen this page)

I also tried

Navigator.of(context, rootNavigator: true).pop();
Navigator.pop(context);//pop dialog
Navigator.pop(context);//pop current page
Navigator.of(context).pop();

All of the above is sometimes works sometimes not work.

Why it is so unstable? And what is the correct way to handle dialog and page navigate?

2

Answers


  1.     what about this The Navigator.popUntil 
        
        this method is called with a predicate that matches the current page (specified by its route name), which removes all pages from the navigation stack except for the current page. Finally, the Navigator.pop method is called to remove the current page from the stack and return control to the previous page.
    
     // Dismiss the dialog and current page
      Navigator.popUntil(context, ModalRoute.withName('/'));
     // Replace '/' with the route name of your current page
      Navigator.pop(context);
    

    this works based on the case you mentioned, however, you also need to check the behavior of your page which might have a difference stack flow.

    Login or Signup to reply.
  2. final res = await showDialog(context: context, builder: {
      // pop dialog
      Navigator.pop(context, [true]);
    });
    if (res) {
      // pop current page
      Navigator.pop(context);
    }
    

    The page pop action should depends on the dialog pop action

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