应用已运行且使用Firebase动态链接时,导航无法正常运行

时间:2020-04-07 10:37:38

标签: firebase flutter dart firebase-dynamic-links

我有这个Loader Widget,它检查动态链接并导航到/game route(gamePage() Widget)(如果使用动态链接打开了应用程序)。如果应用是手动打开的,它将导航到/home路线。

我的代码在关闭应用程序然后通过动态链接打开时效果很好。 但是,当该应用程序已经打开并且位于主页上时,由于上下文为空,因此该代码不起作用。

Loader.dart

class Loader extends StatefulWidget {
  @override
  _LoaderState createState() => _LoaderState();
}

class _LoaderState extends State<Loader> {


    Future<void> initDynamicLinks() async {
        final PendingDynamicLinkData data = await FirebaseDynamicLinks.instance.getInitialLink();
        final Uri deepLink = data?.link;
        print(deepLink);
        if (deepLink != null) {
          Navigator.pushReplacementNamed(context, '/game');  //this works because app has just started
        }
        else{
          FirebaseDynamicLinks.instance.onLink(
              onSuccess: (PendingDynamicLinkData dynamicLink) async {
                final Uri deepLink = dynamicLink?.link;

                if (deepLink != null) {
                  print(context);     // prints null
                  Navigator.pushReplacementNamed(context, '/game'); //this does not work because the app is already running and its on /home and context is null
                }
              },
              onError: (OnLinkErrorException e) async {
                print('error Opening the link');
                print(e.message);
              }
          );
          Navigator.pushReplacementNamed(context, '/home');  // load home widget if there are no deep links
        }
      }

    @override
    void initState(){
      super.initState();
      initDynamicLinks();
    }


    @override
    Widget build(BuildContext context) {
       //Loading animation widget code
    }
}

main.dart

void main() {
  runApp(MaterialApp(
    initialRoute: "/load",
    routes:<String, WidgetBuilder> {
      '/load': (context) => Loader(),
      '/home': (context) => Home(),
      '/game': (context) => gamePage(),
    },
  ),
  );
}

我是Flutter的新手,因此感谢您的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

是的,您是正确的。没有上下文就行不通。因此,每次调用深层链接时,您都需要提供一个上下文。

要实现此目的,请为管理深层链接创建一个自定义类。然后,您可以通过提供上下文来启动该深层链接。

请参见下面的示例。

客户类别

class DynamicLinkService {

  Future handleDynamicLinks(BuildContext context) async {
    final PendingDynamicLinkData data =
    await FirebaseDynamicLinks.instance.getInitialLink();

    _handleDeepLink(data, context);

    FirebaseDynamicLinks.instance.onLink(
        onSuccess: (PendingDynamicLinkData dynamicLink) async {
          // handle link that has been retrieved
          _handleDeepLink(dynamicLink, context);
        }, onError: (OnLinkErrorException e) async {
      print('Link Failed: ${e.message}');
    });
  }

  void _handleDeepLink(PendingDynamicLinkData data, BuildContext context) {
    final Uri deepLink = data?.link;
    if (deepLink != null) {
      print('_handleDeepLink | deeplink: $deepLink');

      var isPost = deepLink.pathSegments.contains('post');
      var isInvite = deepLink.pathSegments.contains('invite');
      if(isInvite){
        Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) =>
            NavigationHomeScreen()), (Route<dynamic> route) => false);
      }
    }
  }
}

从您的加载程序窗口小部件启动深层链接

class Loader extends StatefulWidget {
  @override
  _LoaderState createState() => _LoaderState();
}

class _LoaderState extends State<Loader> {

  final DynamicLinkService _dynamicLinkService = DynamicLinkService();

    @override
    void initState(){
      super.initState();
      _dynamicLinkService.handleDynamicLinks(context);
    }


    @override
    Widget build(BuildContext context) {
       //Loading animation widget code
    }
}