Alexander Skvortsov
Alexander Skvortsov

Reputation: 2764

Flutter `FirebaseDynamicLinks.onLink` calls `onSuccess` twice when the app is in background

in pubspec.yaml:

firebase_dynamic_links: ^0.5.0+11

The code:

    FirebaseDynamicLinks.instance.onLink(
        onSuccess: (PendingDynamicLinkData dynamicLink) async {
          final Uri deepLink = dynamicLink?.link;
          if (deepLink != null) {
            onLinkReceived(deepLink);
          }
        },
        onError: onError);
  }
  1. The app is launched and in background.
  2. User clicks the link.

Actual result: onSuccess is being called twice for the link.

Expected result: onSuccess is being called and being called once per link.

What could be wrong?

Upvotes: 4

Views: 1914

Answers (1)

Alexander Skvortsov
Alexander Skvortsov

Reputation: 2764

So, seems like it is a known issue.

While we are waiting for the fix on the stable channel I am using as a workaround some sort of double call filter which looks somewhat like this:

class DoubleCallFilter<T> {
  T _lastValue;
  int _lastValueTime = 0;
  final int timeoutMs;
  final Future Function(T) action;

  DoubleCallFilter({@required this.action, this.timeoutMs = 500});

  Future<dynamic> call(T value) async {
    final currentTime = DateTime.now().millisecondsSinceEpoch;
    if (_lastValue == value) {
      if (currentTime - _lastValueTime <= timeoutMs) {
        return;
      }
    }
    _lastValue = value;
    _lastValueTime = currentTime;
    if (action != null) await action(_lastValue);
  }
}

And the usage:

    FirebaseDynamicLinks.instance.onLink(
      onSuccess: DoubleCallFilter<PendingDynamicLinkData>(
        action: (dynamicLink) async {
          final Uri deepLink = dynamicLink?.link;
          if (deepLink != null) {
            await onLinkReceived(deepLink);
          }
        },
      ),
      onError: onError,
    );

One of the downsides this solution is that the latest emitted value is being kept in the memory as long as entire DoubleCallFilter is not destroyed (gc'ed).

Upvotes: 1

Related Questions