added error handling for invalid urls
This commit is contained in:
@@ -45,8 +45,9 @@ class _CollectionPageState extends State<CollectionPage> {
|
|||||||
selectedBookmark: selectedBookmark,
|
selectedBookmark: selectedBookmark,
|
||||||
onSavePressed: onBookmarkSaved,
|
onSavePressed: onBookmarkSaved,
|
||||||
onDeletePressed: () {
|
onDeletePressed: () {
|
||||||
Storage.deleteBookmarkById(selectedBookmark.id);
|
Storage.deleteBookmarkById(
|
||||||
setState(() {});
|
selectedBookmark.id,
|
||||||
|
).whenComplete(() => setState(() {}));
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -60,7 +61,11 @@ class _CollectionPageState extends State<CollectionPage> {
|
|||||||
Widget bookmarksListItemBuilder(BuildContext context, Bookmark bookmark) {
|
Widget bookmarksListItemBuilder(BuildContext context, Bookmark bookmark) {
|
||||||
return ListTile(
|
return ListTile(
|
||||||
title: Text(bookmark.name),
|
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),
|
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,
|
selectedCollection: selectedCollection,
|
||||||
onSavePressed: onCollectionSaved,
|
onSavePressed: onCollectionSaved,
|
||||||
onDeletePressed: () {
|
onDeletePressed: () {
|
||||||
Storage.deleteCollection(selectedCollection);
|
Storage.deleteCollection(
|
||||||
setState(() {});
|
selectedCollection,
|
||||||
|
).whenComplete(() => setState(() {}));
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -79,7 +80,9 @@ class _CollectionsListPageState extends State<CollectionsListPage> {
|
|||||||
addingNewBookmark = provider.currentMapsLinkMetadata != null;
|
addingNewBookmark = provider.currentMapsLinkMetadata != null;
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: addingNewBookmark ? Text('Choose Collection') : null,
|
title: addingNewBookmark
|
||||||
|
? Text('Choose Collection')
|
||||||
|
: Text('Collections'),
|
||||||
actions: [
|
actions: [
|
||||||
if (addingNewBookmark)
|
if (addingNewBookmark)
|
||||||
TextButton(
|
TextButton(
|
||||||
@@ -92,11 +95,15 @@ class _CollectionsListPageState extends State<CollectionsListPage> {
|
|||||||
onPressed: onAddButtonPressed,
|
onPressed: onAddButtonPressed,
|
||||||
child: Icon(Icons.add),
|
child: Icon(Icons.add),
|
||||||
),
|
),
|
||||||
body: ListView.builder(
|
body: collections.isNotEmpty
|
||||||
itemBuilder: (context, index) =>
|
? ListView.builder(
|
||||||
collectionsListItemBuilder(context, collections.elementAt(index)),
|
itemBuilder: (context, index) => collectionsListItemBuilder(
|
||||||
itemCount: collections.length,
|
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';
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
|
||||||
Future<void> launchUrlFromString(String url) async {
|
Future<UrlLaunchErrorCode> launchUrlFromString(String url) async {
|
||||||
final Uri uri = Uri.parse(url);
|
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)) {
|
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