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

Flutter中如何在Navigation 1.0架构下处理Deep Links?

1. 配置Android端Manifest

  • 打开android/app/src/main/AndroidManifest.xml,在目标Activity标签内添加intent-filter:
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <!-- 替换为你的自定义scheme和host,示例格式:myapp://example.com -->
    <data
        android:scheme="myapp"
        android:host="example.com" />
</intent-filter>

2. 配置iOS端Info.plist

  • 打开ios/Runner/Info.plist,添加URL Types相关配置:
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myapp</string> <!-- 和Android端scheme保持一致 -->
        </array>
    </dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>myapp</string>
</array>
  • 在main.dart中通过WidgetsBinding获取启动时的Deep Link,结合全局导航Key实现页面跳转:
// 全局导航Key,用于无上下文时跳转页面
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

void main() {
  runApp(const MyApp());
  // 获取启动时的Deep Link
  final initialLink = WidgetsBinding.instance.platformDispatcher.defaultRouteName;
  if (initialLink != '/') {
    handleDeepLink(initialLink);
  }
}

// 解析并处理Deep Link路径
void handleDeepLink(String link) {
  final uri = Uri.parse(link);
  // 示例:处理myapp://example.com/detail/123这类路径
  if (uri.pathSegments.isNotEmpty && uri.pathSegments[0] == 'detail') {
    final id = uri.pathSegments[1];
    navigatorKey.currentState?.push(
      MaterialPageRoute(
        builder: (context) => DetailPage(id: id),
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
      initialRoute: '/',
      routes: {
        '/': (context) => const HomePage(),
      },
    );
  }
}

Android端

  • 打开android/app/src/main/kotlin/你的包名/MainActivity.kt,重写onNewIntent方法传递链接给Flutter:
override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    intent?.data?.let { uri ->
        MethodChannel(flutterEngine?.dartExecutor?.binaryMessenger, "deep_link_channel")
            .invokeMethod("handleDeepLink", uri.toString())
    }
}

iOS端

  • 打开ios/Runner/AppDelegate.swift,重写open方法传递链接给Flutter:
override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let channel = FlutterMethodChannel(name: "deep_link_channel", binaryMessenger: window?.rootViewController as! FlutterBinaryMessenger)
    channel.invokeMethod("handleDeepLink", arguments: url.absoluteString)
    return true
}

Flutter端监听

  • 在main.dart中通过MethodChannel接收后台唤醒的Deep Link:
void main() {
  runApp(const MyApp());
  // ... 其他初始化代码
  const deepLinkChannel = MethodChannel('deep_link_channel');
  deepLinkChannel.setMethodCallHandler((call) async {
    if (call.method == 'handleDeepLink') {
      final link = call.arguments as String;
      handleDeepLink(link);
    }
  });
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 10:22:50