Search code examples
flutter-webflutter-deep-link

Deeplinking in Flutter with GoRouter does not work when using PathUrlStrategy


In the GoRouter I have defined a path for deeplinking:

static const String DEEP_LINK = "deeplink/:someID/:someName";

It is working in the browser as expeced using

http://localhost:12345/#/deeplink/1/test

but when I use

setUrlStrategy(PathUrlStrategy());

to remove the has and enter

http://localhost:12345/deeplink/1/test

it does not work anymore and I get the error:

Could not navigate to initial route.
The requested route name was: "/deeplink/1/test"
There was no corresponding route in the app, and therefore the initial route specified will be ignored and "/" will be used instead.

What am I doing wrong?


Solution

  • After you use setUrlStrategy() or usePathUrlStrategy(), flutter will be configured to use path url strategy like any other website. The web server will try to find the deeplink/1/test file but when you build your app for web with flutter it will just provide the index.html, so the web server will give this error.

    You need to configure your webserver to rewrite requests to index.html. The configuration will depend on which webserver you are using (https://docs.flutter.dev/ui/navigation/url-strategies).