'Flutter use context when handling dynamic link from app closed

I see this weird behaviour when handling dynamic links. What I want to do is that when coming from a link containing matchId parameter I want to clean up the navigation stack, put up the AvailableMatches page and after the MatchDetails page and finally show a modal. This is the code I use in the link handler

  FirebaseDynamicLinks.instance.onLink(
    onSuccess: (PendingDynamicLinkData dynamicLink) async {
        final Uri deepLink = dynamicLink?.link;
        if (deepLink != null) {
           handleLink(deepLink);
        }); 

  Future<void> handleLink(Uri deepLink) async {
      var context = navigatorKey.currentContext;
      var matchId = deepLink.queryParameters["match_id"];
   
      Navigator.of(context).pushNamedAndRemoveUntil(
         AvailableMatches.routeName,
        (Route<dynamic> route) => false
      );
      Navigator.of(context).pushNamed(MatchDetails.routeName,
         arguments: ScreenArguments(
            matchId, false)
      );
      await showModalBottomSheet(context: context, builder: (context) => Text("done"));
  }

If the app is already open this works fine. If the app is starting from this link I have the following initState in the first StatefulWidget

      void initState() {
        super.initState();
        initDynamicLinks();
        loadData(context);
      }
      
      Future<void> loadData(BuildContext context) async {
          final PendingDynamicLinkData data = await FirebaseDynamicLinks.instance.getInitialLink();
          handleLink(data.link);
      }

In this case the handeLink method works until the latest model. The pages are pushed correctly on the stack however the last model doesn't show up.

I am adding prints and things like that but it seems that this line never gets executed. There is no crash or exception. It just gets ignored



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source