3

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 Answers2

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

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • Thanks for your help, But I actually don't show the dialog using the router, I use the `showDialog()` method which I believe uses the `Navigator` to push the dialog. I updated my question. So, I can't check for the location as I don't link the dialog to any route. – Khalid Muhammad Jan 11 '23 at 00:35
1

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.

Wesley Barnes
  • 554
  • 4
  • 18