Flutter: How to handle my AppLinking, Navigation Handling, and Resumption and Creation

38 Views Asked by At

I am implementing Applinking into my application. I have implemented the website part, .assetlinks.json & stuff, and it's all working

On my application, I have added all necessary codes into my manifest file, for I am working on Android application on in Flutter.

So, when I open search my link in Google search bar, it's redirecting me to my application, which is great - it's working! I am also getting the URL of the incoming request.

But, there is an issue, the listener either only listens to incoming link for the first time and then open/creates my app, or, it only listen and resumes my application. But, isn't performing both tasks!

Package : uni_links: ^0.5.1 Framework : Flutter

@override
  void initState() {
    print("Printer");
    WidgetsBinding.instance.addObserver(this);
    colorProvider = Provider.of<themeProvider>(context, listen: false);
    Future.delayed(const Duration(milliseconds: 2500), () async {
      setState(() {
        isExpanded = false;
        isLoading = true;
        isLoading2 = true;
      });
      _handleIncomingLinks();

    });
    super.initState();
  }

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

// ------ AppLinking ------- //
  late StreamSubscription _sub;
  void _handleIncomingLinks() async {
    print('got Navigator');

    _sub = uriLinkStream.listen((Uri? uri) {
      if ((uri?.toString() ?? "").trim().isNotEmpty) {
        String path = uri!.toString();

        if (path.contains('/view/l?id')) {
          String lID = path.split("/view/l?id=").last.trim();
          print("llllID : ${lID}");
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => app_linking_l(l: lID)),
          );
        } else if (path.contains('/view/h?id')) {
          String hID = path.split("/view/h?id=").last.trim();
          print("hhhID : ${hID}");
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => app_linking_h(h: hID)),
          );
        }
      } else {
        print('URi Empty');
      }
    }, onError: (Object err) {
      print('Error: $err');
    });

    await Future.delayed(const Duration(milliseconds: 1));
    if (mounted) {
      print("No Data Found!");
      await checkAuthentication();
    }
  }
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      _handleIncomingLinks();
    }
  }

0

There are 0 best solutions below