added error handling for invalid urls
This commit is contained in:
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user