Search code examples
flutterdartdeep-linkingapplinksflutter-go-router

GoRouter always opens root route on tapping app link


I have implemented app links in Flutter by following this doc. I have tested my app on Android and tapping on the app link always leads me to the root route, which is my app's home page.

I tested the same link on the web and the navigation worked fine and it took me to the route requested by the app link. I had to add /#/ at the end of the url for web.

The GoRouter config works fine on the web but fails on Android.

Do I need to manage routes inside the assetlinks.json file?

This is my router's config

 GoRouter(
  routes: [
    GoRoute(
      path: '/',
      builder: (_, __) => Scaffold(
        appBar: AppBar(title: const Text('Home Screen')),
      ),
      routes: [
        GoRoute(
          path: 'dashboard',
          builder: (_, __) => Scaffold(
            appBar: AppBar(title: const Text('Dashboard Screen')),
          ),
        ),
      ],
    ),
  ],
)

Solution

  • It worked by defining android:pathPrefix="/" inside the data tag in the intent-filter.

    <meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
                <intent-filter android:autoVerify="true">
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data android:scheme="http" android:host="example.com" android:pathPrefix="/"/>
                    <data android:scheme="https" />
                </intent-filter>
    

    For Web

    The URL mydomain/dashboard didn't work on the web app because flutter web has a # in the URL, so to open any route other than / on the web the link should have an extra /#/.

    So this would need separate URLs for Android and web to work correctly. This package helps to remove that # from the url on web