β¨ Introduction
Are you building your first Flutter app and wondering how to store data or let users log in? Look no further! In this guide, youβll learn how to connect Supabase with Dart, enabling login/signup and full CRUD (Create, Read, Update, Delete) operations with just a few lines of code.
By the end, youβll build a mini Notes App where users can sign up, log in, and manage their personal notes. π¬
π€ What is Supabase? (With Analogy)
Think of Supabase as the βopen-source Firebase.β Imagine you’re building a digital notebook. Youβll need:
- A shelf (database)
- A lock on the notebook (authentication)
- A helper to keep everything synced (real-time updates)
- A backend brain (API)
Supabase gives you all of that β without setting up your own servers. It’s:
- Open-source β
- Self-hostable β
- Easy for Flutter devs β
Core features include:
- ποΈ PostgreSQL database
- π Auth (email/password, OAuth)
- π RESTful & GraphQL API
- π Real-time subscriptions
- π Storage & Edge functions
π‘ Why Dart Developers Should Use Supabase
Flutter is powered by Dart, and with the supabase_flutter
package, integrating Supabase is smooth and intuitive. Here’s why it’s perfect for beginners:
- No backend setup needed
- Easy Dart API for login, queries, and real-time
- Great for MVPs, student projects, or full production apps
π οΈ Setup Guide
Letβs walk through setting up Supabase.
1. Create Supabase Project
- Go to https://supabase.com/
- Create an account β Start a new project
- Choose organization name, password, and region
- Once created, go to Project Settings > API:
- Copy your URL and anon key π
2. Enable Auth
- Go to Authentication > Settings
- Enable Email/Password login
3. Create a Table (notes
)
- Go to Table Editor
- Create a table called
notes
:
id: uuid (primary key, default `uuid_generate_v4()`)
user_id: uuid
title: text
content: text
created_at: timestamp (default `now()`)
- Enable Row Level Security (RLS)
- Add this policy:
SELECT, INSERT, UPDATE, DELETE ON notes
TO authenticated
USING (auth.uid() = user_id)
π§βπ» Project Setup in Dart (Flutter)
1. Create Flutter Project
flutter create supabase_notes
cd supabase_notes
2. Add Dependencies
In pubspec.yaml
:
dependencies:
flutter:
sdk: flutter
supabase_flutter: ^1.10.17
Run:
flutter pub get
3. Initialize Supabase
In main.dart
:
import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
void main() async {
await Supabase.initialize(
url: 'https://your-project.supabase.co',
anonKey: 'your-anon-key',
);
runApp(MyApp());
}
π User Login and Signup
Signup:
final response = await Supabase.instance.client.auth.signUp(
email: 'test@example.com',
password: 'password123',
);
Login:
final response = await Supabase.instance.client.auth.signInWithPassword(
email: 'test@example.com',
password: 'password123',
);
Check Logged In User:
final user = Supabase.instance.client.auth.currentUser;
Supabase stores a token locally, so users stay logged in after restarting the app!
π CRUD Operations
Letβs manage notes! Each function here uses notes
table created earlier.
β Create Note
final user = Supabase.instance.client.auth.currentUser;
await Supabase.instance.client.from('notes').insert({
'user_id': user!.id,
'title': 'First Note',
'content': 'Hello Supabase!',
});
π Read Notes
final response = await Supabase.instance.client
.from('notes')
.select()
.eq('user_id', Supabase.instance.client.auth.currentUser!.id);
final notes = response;
βοΈ Update Note
await Supabase.instance.client
.from('notes')
.update({'title': 'Updated Title'})
.eq('id', noteId);
ποΈ Delete Note
await Supabase.instance.client
.from('notes')
.delete()
.eq('id', noteId);
π‘ Real-World Example: Simple Notes App
Imagine this user flow:
- User opens the app β sees login/signup screen
- After login, they see a list of their own notes
- They can create, update, delete their notes
- Other users cannot see each otherβs data (thanks to Row Level Security)
π₯ Bonus Tips
π§― Error Handling
try {
final res = await Supabase.instance.client.from('notes').select();
print(res);
} catch (e) {
print('Something went wrong: $e');
}
π Keep Keys Safe
- Never expose
serviceRole
key - Use
.env
withflutter_dotenv
for managing secrets
β Conclusion
Congrats! π Youβve just learned:
- What Supabase is and why itβs great for Flutter
- How to set up Supabase + Dart
- Auth and CRUD operations
- A working Notes app example
π What to Learn Next?
- Row-Level Security in depth
- Supabase Functions (PostgreSQL or Edge)
- Realtime data subscriptions
- Self-hosting Supabase with Docker
π¦ Helpful Links