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

@@ -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)));
}
}

View File

@@ -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!')),
);
}
}