launch static method
Future<bool>
launch(
- Uri uri, {
- List<
String> ? allowedSchemes, - LaunchMode mode = LaunchMode.platformDefault,
Launches the given uri using the system's preferred method.
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
static Future<bool> launch(
Uri uri, {
List<String>? allowedSchemes,
LaunchMode mode = LaunchMode.platformDefault,
}) async {
final uriString = uri.toString();
developer.log('Attempting to launch: $uriString', name: 'Launchify');
// 1. Internal Validation
if (!UrlValidator.isValid(uri, allowedSchemes: allowedSchemes)) {
developer.log(
'Validation failed for: $uriString ${allowedSchemes != null ? "(Allowed schemes: $allowedSchemes)" : ""}',
name: 'Launchify',
level: 900, // Warning
);
return false;
}
// 2. Platform canLaunch check
try {
final canLaunch = await canLaunchUrl(uri);
if (!canLaunch) {
developer.log(
'Platform reported it cannot launch: $uriString. This usually means the app is not installed or scheme is not registered.',
name: 'Launchify',
level: 900,
);
return false;
}
// 3. Platform launch
final success = await launchUrl(uri, mode: mode);
if (success) {
developer.log('Successfully launched: $uriString', name: 'Launchify');
} else {
developer.log(
'Platform failed to launch: $uriString',
name: 'Launchify',
level: 1000,
);
}
return success;
} catch (e, stack) {
developer.log(
'Critical error during launch: $e',
name: 'Launchify',
error: e,
stackTrace: stack,
level: 1000,
);
return false;
}
}