avioli / uni_links

Flutter plugin for accepting incoming links.
BSD 2-Clause "Simplified" License
564 stars 316 forks source link

Does not work with finished application #127

Open reyesmfabian opened 3 years ago

reyesmfabian commented 3 years ago

I configured everything according to the documentation and it works fine, except that when the application is finished the link does not allow navigation. To make it work I have to open the application first and then click on the link.

main.dart


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  var delegate = await LocalizationDelegate.create(
      fallbackLocale: 'en_US', supportedLocales: ['en_US', 'es']);
  checkDeepLink(delegate);
  runApp(LocalizedApp(delegate, MyApp()));
}

Future checkDeepLink(LocalizationDelegate delegate) async {
  StreamSubscription _sub;
  try {
    await UniLink.getInitialLink();
    _sub = UniLink.uriLinkStream.listen((Uri uri) {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(LocalizedApp(delegate, MyApp()));
    }, onError: (err) {
      // Handle exception by warning the user their action did not succeed
      _sub.cancel();
      print("onError");
    });
  } on PlatformException {
    print("PlatformException");
    _sub.cancel();
  } on Exception {
    print('Exception thrown');
    _sub.cancel();
  }
}

home.dart

Here I read the stream and generate the navigation


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

void deepNavigator(context) {
    _sub = uriLinkStream.listen((Uri uri) {
      if (uri != null) {
        var query = Map.from(uri.queryParameters);
        String id = query['id'];
        Navigator.pushNamedAndRemoveUntil(
              context, 'deepProduct', ModalRoute.withName('home'),
              arguments: id);
      }
    }, onError: (err) {
      print(err);
    });
  }
keskink commented 3 years ago

I added a global variable. Listener start to work after init. But you should get uniLink before listener. getInitialLink() or getInitialUri() will help you. You can manage url or urllink in your app.

Future checkDeepLink(LocalizationDelegate delegate) async { StreamSubscription _sub; try { await UniLink.getInitialLink(); _sub = UniLink.uriLinkStream.listen((Uri uri) { WidgetsFlutterBinding.ensureInitialized(); runApp(LocalizedApp(delegate, MyApp())); }, onError: (err) { // Handle exception by warning the user their action did not succeed _sub.cancel(); print("onError"); });

/////// please add this code here try { uniLink = await getInitialLink(); //uniLink is global variable for use in initstate(). you can check when uiLink is empty/not. } on PlatformException { uniLink = 'Failed to get initial link.'; } on FormatException { uniLink = 'Failed to parse the initial link as Uri.'; } //////

} on PlatformException { print("PlatformException"); _sub.cancel(); } on Exception { print('Exception thrown'); _sub.cancel(); } }

Luciferx86 commented 3 years ago

I added a global variable. Listener start to work after init. But you should get uniLink before listener. getInitialLink() or getInitialUri() will help you. You can manage url or urllink in your app.

Future checkDeepLink(LocalizationDelegate delegate) async { StreamSubscription _sub; try { await UniLink.getInitialLink(); _sub = UniLink.uriLinkStream.listen((Uri uri) { WidgetsFlutterBinding.ensureInitialized(); runApp(LocalizedApp(delegate, MyApp())); }, onError: (err) { // Handle exception by warning the user their action did not succeed _sub.cancel(); print("onError"); });

You are calling runApp() in the stream's listen method? Everytime a link opens the app, when the app is in background, it will cal the runApp() method.