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

Flutter GoRouter插件:Firebase Deeplink后台唤起无法跳转指定页面

解决GoRouter后台状态下Deeplink不跳转的问题

问题原因

应用处于后台时,GoRouter不会自动响应新的Deeplink请求,需手动监听应用唤起事件并主动触发路由跳转。

解决方案

让你的页面State混入WidgetsBindingObserver,监听应用从后台回到前台的状态,获取当前Deeplink并执行跳转。直接修改你的现有代码:

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  final GoRouter _router = GoRouter(
    routes: <RouteBase>[
      GoRoute(
        path: '/',
        builder: (BuildContext context, GoRouterState state) {
          return MyHomePage(title: "", analytics: analytics, observer: observer);
        },
        routes: <RouteBase>[
          GoRoute(
              path: 'map',
              builder: (BuildContext context, GoRouterState state) {
                return MyHomePage(
                    title: "", analytics: analytics, observer: observer);
              }),
          GoRoute(
              path: 'cramond-island',
              builder: (BuildContext context, GoRouterState state) {
                return MyHomePage(
                    title: "Cramond", analytics: analytics, observer: observer);
              }),
        ],
      ),
    ],
  );

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    // 应用从后台回到前台时处理Deeplink
    if (state == AppLifecycleState.resumed) {
      _handleDeeplink();
    }
  }

  void _handleDeeplink() {
    final uri = Uri.base;
    // 排除根路径,避免重复跳转
    if (uri.path != '/' && uri.path.isNotEmpty) {
      _router.go(uri.path);
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Love Edinburgh',
      routerConfig: _router,
    );
  }
}

2. 可选:用uni_links包增强Deeplink监听稳定性

如果Uri.base无法稳定获取后台唤起的Deeplink,可借助uni_links包实现实时监听:

  • 先添加依赖到pubspec.yaml:
dependencies:
  uni_links: ^0.5.1
  • 修改监听逻辑:
import 'package:uni_links/uni_links.dart';

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  StreamSubscription? _linkSubscription;
  final GoRouter _router = GoRouter(...); // 保留你的原有路由配置

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    _initLinkListener();
  }

  @override
  void dispose() {
    _linkSubscription?.cancel();
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  void _initLinkListener() {
    // 监听实时Deeplink变化
    _linkSubscription = uriLinkStream.listen((Uri? uri) {
      if (uri != null && uri.path.isNotEmpty) {
        _router.go(uri.path);
      }
    });
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {
      // 额外检查一次,避免遗漏唤起时的Deeplink
      _checkInitialLink();
    }
  }

  void _checkInitialLink() async {
    final uri = await getInitialUri();
    if (uri != null && uri.path.isNotEmpty) {
      _router.go(uri.path);
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Love Edinburgh',
      routerConfig: _router,
    );
  }
}

关键说明

  • 应用后台唤起时,系统不会自动触发GoRouter的路由解析流程,必须手动获取Deeplink并调用_router.go()完成跳转。
  • Uri.base能覆盖大多数场景,若遇到兼容性问题,uni_links包可提供更可靠的跨平台监听能力。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 21:22:57