Flutter deep link with uni_link and go_router not working correctly on Android

1k views Asked by At

I am using the uni_links package together with the go_router in my app and my deep links are opening the app. I used this method to retrieve the deep link:

  Future<void> initUniLinks() async {
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      final initialLink = await getInitialLink();
      // Parse the link and warn the user, if it is not correct,
      // ...
      context.go('someLocation');
    } on PlatformException {
      // Handle exception by warning the user their action did not succeed
      // return?
    }
  }

That is working perfectly fine on iOS. However on Android when opening a Deep Link, it automatically takes the link as a path and go_router tries to navigate to the location, which fails because the deep link is not an actual location. I am creating the location from the deep link, but there is also other information in that link so the link should not be the exact path.

It is also calling initUniLink and going to the location (context.go(someLocation) in the code above is called. But simultaneously go_router fails and the errorBuilder is triggered, since the path didn't match any location.

What am I doing wrong here? Why is Android using the link as path right away? How can I avoid that?

My only idea was to catch the link inside the routers redirect and check if it is a deep link. If so, I could simply navigate the user to the HomeView. And initUniLink will work anyway. But that feels wrong, since go_router shouldn't even be called to push to the location from the deep link.

Any idea? Happy for every help. Let me know if you need any more info!

1

There are 1 answers

2
Imran Niaz On
**<activity
    android:name=".MainActivity"
    android:launchMode="singleTop"
    android:label="Your App">
    <intent-filter android:label="filter_react_native">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:scheme="your_scheme"
            android:host="your_host"
            android:pathPrefix="/your_path_prefix" />
    </intent-filter>
</activity>

** Configure Deep Links in AndroidManifest.xml:

final goRouter = GoRouter(
  navigatorKey: goRouterKey,
  navigator: Navigator(onGenerateRoute: goRouter.goRouterFunc),
  initialLocation: "/",
  errorBuilder: (BuildContext context, String location) {
    // Check if the location is a deep link, and navigate accordingly
    if (isDeepLink(location)) {
      context.go('HomeView');
    } else {
      // Handle other errors
    }
  },
);

Evaluate go_router Necessity: Assess whether go_router causes conflicts with Android deep link handling. Consider using default Flutter navigation if go_router is not essential.

Custom Redirect Handling in go_router: