skip to Main Content

I was using Navigator 1 then I migrated to go_router to support deep links and web.
Sometimes when I send HTTP requests, I show a loading dialog using showDialog() until the response is processed, after processing I need to check if a dialog is shown or not, if shown I dismiss it using Navigator.of(context).pop().

When I was using Navigator 1, I used to do that in this way:
if (ModalRoute.of(context)?.isCurrent == false) Navigator.of(context).pop();
But now after migrating to go_router this doesn’t work as I found that ModalRoute.of(context) always equals to null whether there is a dialog shown or not.

I tried using if (Navigator.of(context).canPop()) Navigator.of(context).pop();, but this doesn’t differentiate between dialogs and screens, if there is no dialog shown it pops the current screen.

2

Answers


  1. You can do this by checking the location of GoRouter

    if (GoRouter.of(context).location == '/dialog'){ 👈 Check here
        context.pop();
    }
    

    Assuming your dialog route has path /dialog

    Login or Signup to reply.
  2. I just use context.pop() on dialog dismiss.

    Previously did it this way, but tends to throw.

    Set a Global variable:

    GlobalKey<NavigatorState> navKey = GlobalKey<NavigatorState>();
    

    In GoRouter specify:

    navigatorKey: navKey
    

    Pop dialog the ‘old’ way with:

    Navigator.pop(navKey.currentContext!);
    

    Correct me if it gives issues please.

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