how to handle deeplink with AutoRoute in flutter?

211 Views Asked by At

I'm trying to handle a deeplink using the autoRoute package, how can I make my deeplink work when the url is opened?

here is my deeplink handler code

MaterialApp.router(
        title: 'Flutter Demo',
        routerConfig: _appRouter.config(
          navigatorObservers: () => [AutoRouteObserver()],
    deepLinkBuilder: (link) {
            print(link.path);
            if(link.path.startsWith('/ru/reset-password')) {
              return const DeepLink(
                [ResetPassRoute()]
              );
            } else {
              return DeepLink.defaultPath;
            }
    }
        ),
      ),


/// route config 
@AutoRouterConfig()
class AppRouter extends _$AppRouter {
  @override
  List<AutoRoute> get routes => [
        AutoRoute(page: MainRoute.page, initial: true, children: [
          AutoRoute(page: HomeRoute.page),
          AutoRoute(page: HelpRoute.page),
          AutoRoute(page: AboutRoute.page),
          AutoRoute(page: ContactsRoute.page),
          AutoRoute(page: ConfidenceRoute.page),
          AutoRoute(page: TermsRoute.page),
          AutoRoute(page: FavoriteRoute.page),
          AutoRoute(page: CreateRoute.page),
          AutoRoute(page: MessageRoute.page),
          AutoRoute(page: ProfileRoute.page),
          AutoRoute(page: RegistrationRoute.page),
        ]),
        AutoRoute(page: RegistrationRoute.page),
        AutoRoute(page: ResetPassRoute.page),
      ];
}

when I open my url my-url/ru/reset-password nothing happend, how can i fix it?

2

There are 2 best solutions below

0
George Hudson On

Make sure you've setup your AndroidManifest.xml and Info.plist files to handle the deep links.

For AndroidManifest.xml you can add something like this:

<intent-filter>
    <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="my-url" />
</intent-filter>

For Info.plist you can add something like this:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>my-url</string>
        </array>
    </dict>
</array>
0
Nikunj Panchal On

Please check your condition and try this :

   MaterialApp.router(
        title: 'Flutter Demo',
        routerConfig: _appRouter.config(
          navigatorObservers: () => [AutoRouteObserver()],
        deepLinkBuilder: (link) {
            print(link.path);
            if(link.path.contains('/ru/reset-password')) {
              return const DeepLink(
                [ResetPassRoute()]
              );
            } else {
              return DeepLink.defaultPath;
            }
          }
        ),
      ),