launch static method

  1. @Deprecated('Use launchAction instead for more control and explicit launch types.' 'The context parameter is now required for multi-choice dialogs.')
Future<bool> launch(
  1. BuildContext context,
  2. Uri uri, {
  3. List<String>? allowedSchemes,
  4. LaunchMode mode = LaunchMode.platformDefault,
  5. LaunchOptions? options,
})

Launches the given uri using the system's preferred method.

This method is deprecated. Please use launchAction for more control over launch types and fallback mechanisms.

uri - The URI to be launched. allowedSchemes - An optional list of allowed URL schemes for security. mode - How the URL should be opened (e.g., in-app, system browser).

Returns true if successfully launched, false otherwise.

Implementation

@Deprecated(
  'Use launchAction instead for more control and explicit launch types.'
  'The context parameter is now required for multi-choice dialogs.',
)
static Future<bool> launch(
  BuildContext context,
  Uri uri, {
  List<String>? allowedSchemes,
  LaunchMode mode = LaunchMode.platformDefault,
  LaunchOptions? options,
}) async {
  LaunchType type;
  String? value;
  String? emailSubject;
  String? emailBody;
  String? smsBody;

  switch (uri.scheme) {
    case 'tel':
      type = LaunchType.phone;
      value = uri.path;
      break;
    case 'sms':
      type = LaunchType.sms;
      value = uri.path;
      smsBody = uri.queryParameters['body'];
      break;
    case 'mailto':
      type = LaunchType.email;
      value = uri.path;
      emailSubject = uri.queryParameters['subject'];
      emailBody = uri.queryParameters['body'];
      break;
    case 'https':
    case 'http':
      // Check for whatsapp specific URL
      if (uri.host == 'wa.me') {
        type = LaunchType.whatsapp;
        value = uri.pathSegments.isNotEmpty ? uri.pathSegments.first : null;
      } else if (uri.host.contains('instagram.com')) {
        type = LaunchType.instagram;
        value = uri.pathSegments.isNotEmpty ? uri.pathSegments[0] : null;
      } else if (uri.host.contains('tiktok.com')) {
        type = LaunchType.tiktok;
        value = uri.pathSegments.isNotEmpty ? uri.pathSegments[0] : null;
      } else if (uri.host.contains('linkedin.com')) {
        type = LaunchType.linkedin;
        value = uri.pathSegments.isNotEmpty ? uri.pathSegments[0] : null;
      } else if (uri.host.contains('google.com') &&
          uri.path.contains('maps')) {
        type = LaunchType.map;
        value = uri.queryParameters['query'];
      } else {
        type = LaunchType.website;
        value = uri.toString();
      }
      break;
    default:
      type = LaunchType.custom;
      value = uri.toString();
      break;
  }

  return await launchAction(
    context,
    type: type,
    value: value,
    queryParameters: {
      ...uri.queryParameters,
      if (emailSubject != null) 'subject': emailSubject,
      if (emailBody != null) 'body': emailBody,
      if (smsBody != null) 'body': smsBody,
    },
    allowedSchemes: allowedSchemes,
    mode: mode,
    options: null,
  );
}