Flutter 深度链接与 uni_link 和 go_router 在 Android 上无法正常工作

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

提问人:Chris 提问时间:11/12/2023 更新时间:11/21/2023 访问量:87

问:

我正在将 uni_links 包与应用程序中的go_router一起使用,并且我的深层链接正在打开该应用程序。我用这种方法来检索深层链接:

  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?
    }
  }

这在 iOS 上运行良好。但是,在 Android 上,当打开深度链接时,它会自动将链接作为 并尝试导航到该位置,但会失败,因为深层链接不是实际位置。我正在从深层链接创建位置,但该链接中还有其他信息,因此该链接不应是确切的路径pathgo_router

它还调用并转到上述代码中调用的位置(context.go())。但同时go_router失败并被触发,因为路径与任何位置都不匹配。initUniLinksomeLocationerrorBuilder

我在这里做错了什么?为什么 Android 立即使用链接作为路径?我怎样才能避免这种情况?

我唯一的想法是捕获路由器内部的链路并检查它是否是深度链路。如果是这样,我可以简单地将用户导航到 HomeView。无论如何都会起作用。但这感觉不对,因为甚至不应该调用它从深层链接推送到该位置。redirectinitUniLinkgo_router

有什么想法吗?为每一次帮助感到高兴。如果您需要更多信息,请告诉我!

Android Dart 深度链接 flutter-go-router gorouter

评论


答:

-2赞 Imran Niaz #1
**<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>

** 在 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
    }
  },
);

评估go_router必要性:评估go_router是否会导致与 Android 深层链接处理发生冲突。如果 Flutter 导航不是必需的,请考虑使用默认go_router导航。

go_router中的自定义重定向处理:

评论

0赞 Chris 11/21/2023
并不是这个问题的真正答案。 对我来说是必不可少的,您的 cusotm 重定向代码只是我在问题中陈述的想法的代码版本。go_router
0赞 Community 11/22/2023
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。