how to add a bookmark feature to a flutter application
How to Add a Bookmark Feature to a Flutter Application
Have you ever read an interesting article and wanted to save it so you could read it later?
That is exactly what a bookmark feature does.
Many social media applications, blogging platforms, and reading applications allow users to save their favourite content.
If you are building a Flutter application, adding bookmarks is a useful feature to learn.
In this tutorial, you will learn how to create a simple bookmark system using Flutter and Supabase.
1. What Is a Bookmark Feature?
A bookmark feature allows users to save content they want to access later.
Examples include:
- Saving blog articles.
- Bookmarking social media posts.
- Saving products.
- Keeping useful educational materials.
- Saving interesting videos.
A bookmark system improves the user experience by making important content easier to find.
2. Create a Flutter Project
Create a new project:
flutter create bookmark_app
Install Supabase:
flutter pub add supabase_flutter
Make sure you have already initialized Supabase using your project URL and publishable key.
3. Create a Bookmarks Table
Open your Supabase SQL Editor and create a table:
create table public.bookmarks (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id),
post_id bigint not null references public.posts(id)
on delete cascade,
created_at timestamptz default now(),
unique (user_id, post_id)
);
This table stores the relationship between users and the posts they save.
Enable Row Level Security:
alter table public.bookmarks
enable row level security;
Allow users to view their own bookmarks:
create policy "Users can view their bookmarks"
on public.bookmarks
for select
to authenticated
using (auth.uid() = user_id);
Allow users to save their own bookmarks:
create policy "Users can create their bookmarks"
on public.bookmarks
for insert
to authenticated
with check (auth.uid() = user_id);
Allow users to remove their own bookmarks:
create policy "Users can delete their bookmarks"
on public.bookmarks
for delete
to authenticated
using (auth.uid() = user_id);
These policies prevent users from managing another user's saved records.
4. Create a Bookmark Function
You can save a post using the following Flutter function:
Future<void> saveBookmark(int postId) async {
final user = Supabase.instance.client.auth.currentUser;
if (user == null) {
throw Exception('Please log in first.');
}
await Supabase.instance.client
.from('bookmarks')
.upsert(
{
'user_id': user.id,
'post_id': postId,
},
onConflict: 'user_id,post_id',
);
}
The "upsert()" method helps prevent duplicate bookmark records when the same user saves the same post again.
5. Create a Function to Remove Bookmarks
Users should be able to remove content they no longer want to save.
Example:
Future<void> removeBookmark(int postId) async {
final user = Supabase.instance.client.auth.currentUser;
if (user == null) return;
await Supabase.instance.client
.from('bookmarks')
.delete()
.eq('post_id', postId)
.eq('user_id', user.id);
}
This removes the selected post from the current user's bookmarks.
6. Create a Bookmark Button
You can use Flutter's "IconButton" to create a bookmark button.
IconButton(
onPressed: () async {
await saveBookmark(postId);
},
icon: const Icon(Icons.bookmark_border),
)
You can change the icon to "Icons.bookmark" when the post has already been saved.
A complete interface should also show loading indicators and friendly error messages while saving or removing bookmarks.
7. Retrieve Saved Bookmarks
You can retrieve the current user's saved records:
Future<List<Map<String, dynamic>>> getBookmarks() async {
final user = Supabase.instance.client.auth.currentUser;
if (user == null) {
return [];
}
final response = await Supabase.instance.client
.from('bookmarks')
.select('id, post_id, created_at')
.eq('user_id', user.id)
.order('created_at', ascending: false);
return List<Map<String, dynamic>>.from(response);
}
This returns the saved post IDs.
You can use those IDs to retrieve the corresponding post details from your posts table.
8. Create a Saved Posts Page
A professional application should have a page where users can see their saved content.
For example, your saved posts page can display:
- Article title.
- Article image.
- Short description.
- Date saved.
- Remove bookmark button.
You can create this page using Flutter's "ListView.builder" widget.
For a more advanced application, retrieve the bookmark and post information together using a suitable database relationship query.
9. Test Your Bookmark System
After creating the feature, test the following:
1. Log in to your application.
2. Open a post.
3. Tap the bookmark button.
4. Open the saved posts page.
5. Confirm that the post appears.
6. Remove the bookmark.
7. Confirm that it disappears.
8. Log out and log in again.
9. Check that the saved information remains available.
10. Final Thoughts
A bookmark feature is a useful addition to almost every content-based application.
Flutter allows you to create the interface, while Supabase helps store and manage saved records.
You can use this feature in blogging platforms, social media applications, shopping platforms, and educational applications.
Keep practising and continue adding useful features to your application.
Comments
Post a Comment