added error handling for invalid urls
This commit is contained in:
@@ -45,8 +45,9 @@ class _CollectionPageState extends State<CollectionPage> {
|
||||
selectedBookmark: selectedBookmark,
|
||||
onSavePressed: onBookmarkSaved,
|
||||
onDeletePressed: () {
|
||||
Storage.deleteBookmarkById(selectedBookmark.id);
|
||||
setState(() {});
|
||||
Storage.deleteBookmarkById(
|
||||
selectedBookmark.id,
|
||||
).whenComplete(() => setState(() {}));
|
||||
},
|
||||
),
|
||||
);
|
||||
@@ -60,7 +61,11 @@ class _CollectionPageState extends State<CollectionPage> {
|
||||
Widget bookmarksListItemBuilder(BuildContext context, Bookmark bookmark) {
|
||||
return ListTile(
|
||||
title: Text(bookmark.name),
|
||||
onTap: () => launchUrlFromString(bookmark.link),
|
||||
onTap: () => launchUrlFromString(bookmark.link).then((errorCode) {
|
||||
if (context.mounted && errorCode != UrlLaunchErrorCode.none) {
|
||||
return showUrlError(context, errorCode);
|
||||
}
|
||||
}),
|
||||
onLongPress: () => editBookmark(bookmark),
|
||||
);
|
||||
}
|
||||
@@ -104,4 +109,16 @@ class _CollectionPageState extends State<CollectionPage> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void showUrlError(BuildContext context, UrlLaunchErrorCode errorCode) {
|
||||
String errorText = '';
|
||||
if (errorCode == UrlLaunchErrorCode.couldNotLaunch) {
|
||||
errorText = 'Could not launch Url';
|
||||
} else {
|
||||
errorText = 'Invalid Url';
|
||||
}
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text(errorText)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,8 +66,9 @@ class _CollectionsListPageState extends State<CollectionsListPage> {
|
||||
selectedCollection: selectedCollection,
|
||||
onSavePressed: onCollectionSaved,
|
||||
onDeletePressed: () {
|
||||
Storage.deleteCollection(selectedCollection);
|
||||
setState(() {});
|
||||
Storage.deleteCollection(
|
||||
selectedCollection,
|
||||
).whenComplete(() => setState(() {}));
|
||||
},
|
||||
),
|
||||
);
|
||||
@@ -79,7 +80,9 @@ class _CollectionsListPageState extends State<CollectionsListPage> {
|
||||
addingNewBookmark = provider.currentMapsLinkMetadata != null;
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: addingNewBookmark ? Text('Choose Collection') : null,
|
||||
title: addingNewBookmark
|
||||
? Text('Choose Collection')
|
||||
: Text('Collections'),
|
||||
actions: [
|
||||
if (addingNewBookmark)
|
||||
TextButton(
|
||||
@@ -92,11 +95,15 @@ class _CollectionsListPageState extends State<CollectionsListPage> {
|
||||
onPressed: onAddButtonPressed,
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
body: ListView.builder(
|
||||
itemBuilder: (context, index) =>
|
||||
collectionsListItemBuilder(context, collections.elementAt(index)),
|
||||
itemCount: collections.length,
|
||||
body: collections.isNotEmpty
|
||||
? ListView.builder(
|
||||
itemBuilder: (context, index) => collectionsListItemBuilder(
|
||||
context,
|
||||
collections.elementAt(index),
|
||||
),
|
||||
itemCount: collections.length,
|
||||
)
|
||||
: Center(child: Text('Create your first Collection to get started!')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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