Search code examples
flutterflutter-go-routeruniversal-link

How to pass part of a page path using Go Route when using universal routes?


When using universal links and Go Router if I have a webpage such as www.mywebsite.com/factfile/MickeyMouse please can you tell me how to pass the string "MickeyMouse" to a class? (or how to send the whole page path and I can just extract the string after the forward slash in the class?)

I was thinking something like:

final router = GoRouter(
  routes: [
    GoRoute(
      path: '/',
      builder: (_, __) => Scaffold(
        appBar: AppBar(title: const Text('Home Screen')),
      ),
      routes: [
        GoRoute(
          path: 'factfile/*',
          builder: (_, __) => ShowFactfile(path:path),
          ),
      ],
    ),
  ],
);

Thank you for any pointers,

N


Solution

  • I think you would just use:

    GoRoute(
              path: 'factfile/*',
              builder: (_, state) => ShowFactfile(path: state.path), 
              ),
    

    I got it from the GoRouter documentation. They have more ways of getting the url path other than state.path.

    This will probably give you factfile/MickeyMouse so you can split the string and extract MickeyMouse like:

    state.fullPath.split('/')[1]