You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GoRouter点击App Link始终跳转根路由(Android端)求助

Android端App Link跳转指定路由失效问题排查与解决

核心问题解答

不需要在assetlinks.json中管理路由。这个文件仅用于验证你的应用与目标域名的归属关系,确保只有你的应用能处理该域名的App Link,和路由跳转逻辑完全无关。

问题原因分析

Android端点击App Link跳转到根路由,本质是GoRouter没有正确解析从Intent传递过来的深层链接路径,导致默认 fallback到初始路由/;而Web端的/#/是hash模式路由,GoRouter能直接识别并处理。

具体修复步骤

  1. 完善AndroidManifest.xml的Intent Filter配置
    确保Intent Filter包含正确的域名、路径前缀,并且开启autoVerify:
<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" />
    <!-- 替换为你的实际域名,pathPrefix可指定要处理的路径范围 -->
    <data
        android:scheme="https"
        android:host="your-domain.com"
        android:pathPrefix="/" />
</intent-filter>
  1. 初始化GoRouter时传入正确的初始路径
    从Android Intent中提取深层链接的路径,作为GoRouter的初始路由:
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 获取启动应用的初始链接
  final Uri? initialUri = await getInitialUri();
  String initialLocation = '/';
  if (initialUri != null) {
    // 提取链接中的路径部分
    initialLocation = initialUri.path;
  }

  runApp(MyApp(initialLocation: initialLocation));
}

class MyApp extends StatelessWidget {
  final String initialLocation;

  const MyApp({super.key, required this.initialLocation});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: GoRouter(
        initialLocation: initialLocation,
        routes: [
          GoRoute(
            path: '/',
            builder: (_, __) => const Scaffold(
              appBar: AppBar(title: Text('Home Screen')),
            ),
            routes: [
              GoRoute(
                path: 'dashboard',
                builder: (_, __) => const Scaffold(
                  appBar: AppBar(title: Text('Dashboard Screen')),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
  1. 监听后台唤起的链接(可选)
    如果需要处理应用在后台时点击App Link的场景,添加链接监听:
class _MyAppState extends State<MyApp> {
  StreamSubscription<Uri?>? _uriSubscription;

  @override
  void initState() {
    super.initState();
    // 监听应用从后台被链接唤起的事件
    _uriSubscription = uriLinkStream.listen((Uri? uri) {
      if (uri != null && mounted) {
        context.go(uri.path);
      }
    });
  }

  @override
  void dispose() {
    _uriSubscription?.cancel();
    super.dispose();
  }

  // ... 其余代码
}
  1. 测试注意事项
  • 确保assetlinks.json已部署到域名的.well-known目录,且签名与应用一致(可通过Android Studio的App Link验证工具检查)。
  • 测试需使用签名打包的APK/AB,调试模式下autoVerify可能无法正常工作。

内容的提问来源于stack exchange,提问作者ASAD HAMEED

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 10:25:20