πŸ“š How to Use Supabase with Dart: Login & CRUD Made Easy for Beginners


✨ 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

  1. Go to https://supabase.com/
  2. Create an account β†’ Start a new project
  3. Choose organization name, password, and region
  4. 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:

  1. User opens the app β†’ sees login/signup screen
  2. After login, they see a list of their own notes
  3. They can create, update, delete their notes
  4. 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 with flutter_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


Leave a Reply

Your email address will not be published. Required fields are marked *