shareUri method

  1. @override
Future<ShareResult> shareUri(
  1. Uri uri, {
  2. String? subject,
  3. Rect? shareOrigin,
})
override

Attempts to share a uri via the Web Share API, and falls back to mailto: when not available

cf https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share

No UTM tracking parameters are added to the uri when sharing via the Web Share API, as it provides no info on the selected sharing target.

The shareOrigin is unused on web.

Implementation

@override
Future<ShareResult> shareUri(Uri uri, {String? subject, Rect? shareOrigin}) async {
  final navigator = html.window.navigator;
  try {
    // Attempt to use the Web Share API
    await navigator.share({'url': uri.toString()});
    return ShareResult(true, uri);
  } on NoSuchMethodError catch (_) {
    // Web Share API is unavailable, fall back to mailto:
    // Add the utm_source=mail parameter to the uri
    final mailUri = uri.replace(
      queryParameters: {
        ...uri.queryParameters,
        'utm_source': 'mail',
        'utm_medium': 'email',
      },
    );
    final mailto = Uri(
      scheme: 'mailto',
      queryParameters: {
        if (subject != null) 'subject': subject,
        'body': mailUri.toString(),
      },
    );
    try {
      // Open e-mail client to share the link
      html.window.open(mailto.toString(), '_blank');
      return ShareResult(true, mailUri, target: "mail");
    } catch (e) {
      return ShareResult(false, mailUri);
    }
  }
}