canLaunch static method

Future<bool> canLaunch({
  1. required LaunchType type,
  2. String? value,
  3. Map<String, dynamic>? queryParameters,
})

Determines if a given LaunchType can be handled by the device.

This method checks for the availability of an app to handle the primary URI for the given LaunchType. For social media types, it checks the app-specific scheme. For others, it checks the primary URI.

type - The category of launch to check. value - The primary data string (e.g., URL, phone number, social media handle). queryParameters - Optional map of additional query parameters. For LaunchType.calendar, this can contain a 'calendarEvent' key. For LaunchType.share, this can contain 'text' and 'subject' keys.

Returns true if the action can potentially be launched, false otherwise. For LaunchType.share, it returns true if shareText is not empty.

Implementation

static Future<bool> canLaunch({
  required LaunchType type,
  String? value,
  Map<String, dynamic>? queryParameters,
}) async {
  Uri uri;

  // Extract specific parameters from queryParameters for UrlBuilder
  final CalendarEvent? calendarEvent =
      queryParameters?['calendarEvent'] as CalendarEvent?;
  final String? shareText = queryParameters?['text'] as String?;

  switch (type) {
    case LaunchType.instagram:
      uri = Uri.tryParse(value ?? '') ?? Uri();
      break;
    case LaunchType.tiktok:
      uri = Uri.tryParse(value ?? '') ?? Uri();
      break;
    case LaunchType.linkedin:
      uri = Uri.tryParse(value ?? '') ?? Uri();
      break;
    case LaunchType.facebook:
      uri = Uri.tryParse(value ?? '') ?? Uri();
      break;
    case LaunchType.github:
      uri = Uri.tryParse(value ?? '') ?? Uri();
      break;
    case LaunchType.calendar:
      // For calendar, we build the URI and then check if it can be launched.
      uri = UrlBuilder.buildCalendarUri(calendarEvent);
      break;
    case LaunchType.share:
      // Share functionality is handled by share_plus, which doesn't use url_launcher's canLaunchUrl.
      // We assume it's always possible to share unless the text to share is explicitly empty.
      return shareText != null && shareText.isNotEmpty;
    case LaunchType.map:
      // For map, we build the URI and then check if it can be launched.
      uri = UrlBuilder.buildMapUri(value, queryParameters: queryParameters);
      break;
    default:
      // For other types (website, phone, email, sms, whatsapp, custom), build a generic URI.
      uri = UrlBuilder.buildUri(
        type: type,
        value: value,
        queryParameters: queryParameters,
      );
  }

  // If the URI is empty, it means the UrlBuilder couldn't construct a valid URI,
  // so it cannot be launched.
  if (uri.toString().isEmpty) return false;
  return await canLaunchUrl(uri);
}