added error handling for invalid urls

This commit is contained in:
2026-01-14 20:50:36 +01:00
parent be6a44e7f0
commit c0f92fac58
3 changed files with 44 additions and 14 deletions

View File

@@ -1,8 +1,14 @@
import 'package:url_launcher/url_launcher.dart';
Future<void> launchUrlFromString(String url) async {
final Uri uri = Uri.parse(url);
Future<UrlLaunchErrorCode> launchUrlFromString(String url) async {
final Uri? uri = Uri.tryParse(url);
final isValid =
uri != null && uri.hasAbsolutePath && uri.scheme.startsWith('http');
if (!isValid) return UrlLaunchErrorCode.invalidUrl;
if (!await launchUrl(uri, mode: LaunchMode.externalApplication)) {
throw Exception('Could not launch $url');
return UrlLaunchErrorCode.couldNotLaunch;
}
return UrlLaunchErrorCode.none;
}
enum UrlLaunchErrorCode { none, couldNotLaunch, invalidUrl }