Using uni_links when the app is in the background

Viewed 6681

My application has the following structure:

- InheritedWidget for dependencies
--> Splash Screen Page
--> Login Pages
--> Main Pages

When the app runs for the first time, I can use var link = await getInitialLink(); to get the value for the link that opened the app.

However, I cannot get the same result if I open the app on the background.

I tried to use

getLinksStream().listen((link) => (link) {
          try {
            var _latestUri;
            if (link != null) _latestUri = Uri.parse(link);
            print("=== Formated successfully a link!");
          } on FormatException {
            print("--- A link got here but was invalid");
          }
        });

For getting the link in the Splash Screen, but if the app is already open in the Login or Main pages, it won't go through the Splash Screen again.

Then, I tried to put it in the InheritedWidget, but alas, didn't get any result whatsoever.

So my question is: Where and how should I set up uni_links so that I can catch all incoming links even if the app is open?

Or better, is there an alternative for App Links/Universal Links that I can use?

2 Answers

Though it's not the best and most elegant way, I got around this.

First, I was not using getLinksStream correctly

Instead of

(link) => (link) {...}

It's

(link) {...}

Then, I need to put this subscription in my Splash Screen and do not dispose of it, so that it can listen to new events.

If anybody has a better solution, please free to add it

For this situation, you need to use both of getInitialUri() and getUriLinksStream(), If app launchs directly u will get uri from getInitialUri, if app already running on background u will get uri from getUriLinksStream.

Related