getBookmarks method
Retrieves a list of bookmarks.
This method fetches all the bookmarks stored in the repository.
Returns: A list of BookmarkModel objects representing the bookmarks.
Implementation
List<BookmarkModel> getBookmarks() {
final savedBookmarks = GetStorage().read(_StorageConstants().bookmarks);
if (savedBookmarks == null || savedBookmarks is! List) {
return []; // Return an empty list if data is null or not a list
}
try {
return savedBookmarks.map((bookmark) {
if (bookmark is Map<dynamic, dynamic>) {
// Cast to Map<String, dynamic> before passing to fromJson
return BookmarkModel._fromJson(Map<String, dynamic>.from(bookmark));
} else if (bookmark is String) {
// Decode JSON string and cast to Map<String, dynamic>
return BookmarkModel._fromJson(
Map<String, dynamic>.from(jsonDecode(bookmark)),
);
} else {
throw Exception("Unexpected bookmark type: ${bookmark.runtimeType}");
}
}).toList();
} catch (e) {
// Log the error and return an empty list in case of issues
log("Error parsing bookmarks: $e");
return [];
}
}