llfbandit / app_links

Android App Links, Deep Links, iOs Universal Links and Custom URL schemes handler for Flutter.
https://pub.dev/packages/app_links
Apache License 2.0
220 stars 81 forks source link

On ios _appLinks?.uriLinkStream.listen((Uri? uri) invoked twice #140

Closed iqbbal00 closed 4 months ago

iqbbal00 commented 4 months ago

Describe the bug

linkSubscription = _appLinks?.uriLinkStream.listen((Uri? uri) async {
      logger.i('Received URI: $uri');
      _uniLinkHandler(uri: uri);
    }

this method is invoked twice everytime the app is being called by scheme

To Reproduce Steps to reproduce the behavior:

set up custom scheme calling scheme:// code invoked twice Expected behavior code invoked once should be expected

Smartphone (please complete the following information): iOS iphone 8 etc

my code snipset as below:

Future<void> init() async {
    logger.i('DeepLinksService Initializing');

    try {
      final Uri? initialURI = await _appLinks?.getInitialLink();
      if (_appLinks != null) {
        logger.i("Initial URI received $initialURI");
        _uniLinkHandler(uri: initialURI);
        _initialized = true;z
      }else{
        logger.e("Null Initial URI received");
      }
    } on PlatformException {
      if (kDebugMode) logger.e("(PlatformException) Failed to receive initial uri.");
    } on FormatException catch (error) {
      if (kDebugMode) logger.e("(FormatException) Malformed Initial URI received. Error: $error");
    }

    _linkSubscription = _appLinks?.uriLinkStream.listen((Uri? uri) async {
      logger.i('Received URI: $uri');
      _uniLinkHandler(uri: uri);
    }, onError: (error) {
      // Handle exception by warning the user their action did not succeed
      if (kDebugMode) logger.e('UniLinks onUriLink error: $error');
    }, onDone: () {
      _linkSubscription?.cancel();
    });
  }

This issue is the same as the issue in the previous package : https://github.com/avioli/uni_links/issues/16

llfbandit commented 4 months ago

Your init method receives twice the link because of getInitialLink and stream. Use stream only.