1

I am using https://pub.dev/packages/go_router plugin. When my app is closed and I click on a deeplink it opens my app correctly. If the app is in the background the deeplink merely brings it to the foreground, it does not actually do anything to the app.

I've looked at the other answers on S/O and they have non-specific responses, since the ops didn't post any code. I've skimmed the arguments in GoRoute's code for anything pertaining to 'onGenerate', which S/O answers suggest I need, but I can't find anything.

This is a snippet of the code from main() which receives URL routes and directs my app...

final GoRouter _router = GoRouter(
  routes: <RouteBase>[
    GoRoute(
      path: '/',
      builder: (BuildContext context, GoRouterState state) {
        return MyHomePage(title: "", analytics: analytics, observer: observer);
      },
      routes: <RouteBase>[
        GoRoute(
            path: 'map', // a do nothing router
            builder: (BuildContext context, GoRouterState state) {
              return MyHomePage(
                  title: "", analytics: analytics, observer: observer);
            }),
        GoRoute(
            path: 'cramond-island',
            builder: (BuildContext context, GoRouterState state) {
              return MyHomePage(
                  title: "Cramond", analytics: analytics, observer: observer);
            }),
      ],
    ),
  ],
);

This _router is attached in build() as so...

  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Love Edinburgh',
      routerConfig: _router
  );
}
Sam
  • 1,659
  • 4
  • 23
  • 42

1 Answers1

0

Let's say 'path' is the url/path you receive from dynamic link. Then you can check if the path matches any path of a page you defined in go router. You will also need to create a GlobalKey and assign it the navigatorKey parameter of the GoRoute.

final GlobalKey<NavigatorState> rootNavigatorKey =
    GlobalKey<NavigatorState>(debugLabel: 'root');

final GoRouter _router = GoRouter(
  navigatorKey: rootNavigatorKey,
  routes: <RouteBase>[
    GoRoute(
      path: '/',
      builder: (BuildContext context, GoRouterState state) {
        return MyHomePage(title: "", analytics: analytics, observer: observer);
      },
      routes: <RouteBase>[
        GoRoute(
            path: 'map', // a do nothing router
            builder: (BuildContext context, GoRouterState state) {
              return MyHomePage(
                  title: "", analytics: analytics, observer: observer);
            }),
        GoRoute(
            path: 'cramond-island',
            builder: (BuildContext context, GoRouterState state) {
              return MyHomePage(
                  title: "Cramond", analytics: analytics, observer: observer);
            }),
      ],
    ),
  ],
);

then you can compare the path or uri you receive from dynamic link and compare it to the path you defined in go router like:

if (path == '/map') {
      rootNavigatorKey.currentContext.go('/map');
}

I hope this helps. If you have any doubt, please let me know.

Aditya Arora
  • 351
  • 2
  • 7