Reputation: 2764
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);
}
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
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