Module 4
Topic 1

Supabase Setup

Setting up Supabase — the open-source Firebase alternative — for your Flutter applications.

Supabase Flutter Quickstart supabase_flutter package Flutter SDK Docs
What Is Supabase?

Supabase is an open-source Firebase alternative that provides a full backend solution for your Flutter apps. It's built on top of PostgreSQL , the world's most trusted relational database, and offers:

🔐
Authentication
Email, password, magic links, OAuth providers
🗄️
PostgreSQL Database
Full relational database with realtime subscriptions
📁
Storage
File storage with public and private buckets
🔄
Realtime
Live database changes and presence tracking
🛡️
Row Level Security
Fine-grained access control policies
Edge Functions
Serverless functions with TypeScript support

✅ Why Supabase Over Firebase?

  • PostgreSQL – Real relational database with advanced querying
  • Open Source – Self-hostable, no vendor lock-in
  • Better Pricing – Free tier includes 500MB database, 1GB storage, 2GB bandwidth
  • SQL – Use SQL directly for complex queries
  • Real Realtime – Full CRUD subscriptions, not just Firestore-like
Creating a Supabase Project

To get started with Supabase, you need to create an account and set up your first project.

1.
Sign up – Go to supabase.com and create a free account with GitHub or email.
2.
Create a new project – Click "New Project" and fill in the details:
  • Name – e.g., "my_flutter_app"
  • Database Password – Choose a strong password
  • Region – Choose the closest region to your users
  • Pricing Plan – Free tier works for learning
3.
Wait for setup – Supabase will provision your PostgreSQL database (takes 1-2 minutes).
4.
Get your API keys – Once the project is ready, go to the "Settings" → "API" section. You'll find:
  • Project URL https://your-project.supabase.co
  • anon public key – Used for client-side requests
  • service_role key – Used for server-side/admin operations (keep secret!)

⚠️ Important: API Keys

  • anon key – Safe to use in client-side Flutter apps. This key is limited by Row Level Security policies.
  • service_role key – Never expose this in client-side code! It bypasses all RLS policies.
Connecting Flutter to Supabase

To connect your Flutter app to Supabase, you need to add the supabase_flutter package and configure it with your project credentials.

💡 Using Environment Variables (Recommended)

For production apps, store your Supabase credentials in environment variables:

Then run with: flutter run --dart-define=SUPABASE_URL=your-url --dart-define=SUPABASE_ANON_KEY=your-key

Verifying Your Connection

To verify that your Flutter app is correctly connected to Supabase, you can make a simple query to fetch data from a table.

Step-by-Step Explanation
1.
Add the dependency – Add supabase_flutter to your pubspec.yaml .
2.
Initialize Supabase – Call Supabase.initialize() before running the app, passing your project URL and anon key.
3.
Access the client – Use Supabase.instance.client to access the Supabase client anywhere in your app.
4.
Check authentication state – Use Supabase.instance.client.auth.currentSession to check if a user is logged in.
Recommended Packages

These packages work great with Supabase and are recommended for production apps:

go_router

  • Declarative routing
  • Deep linking support
  • Auth guards
  • Nested routes

flutter_riverpod

  • State management
  • Dependency injection
  • Providers for Supabase
  • AsyncValue handling

freezed

  • Immutable data classes
  • Union types
  • JSON serialization
  • CopyWith methods

cached_network_image

  • Image caching
  • Placeholder widgets
  • Error handling
  • Memory management

📦 Package Versions

  • supabase_flutter: ^2.8.0
  • go_router: ^14.0.0
  • flutter_riverpod: ^2.5.0
  • freezed_annotation: ^2.4.0
  • json_serializable: ^6.7.0
  • cached_network_image: ^3.3.0
Common Mistakes
❌ Mistake 1: Using the service_role key in client-side code

The service_role key bypasses all security policies. Never expose it in your Flutter app.

✅ Correct: Use anon key for client-side

Always use the anon key in your Flutter app. It's safe and respects Row Level Security policies.

❌ Mistake 2: Not initializing Supabase before runApp

If you try to use Supabase before calling Supabase.initialize() , you'll get an error.

✅ Correct: Initialize in main

Always call await Supabase.initialize() before runApp() .

❌ Mistake 3: Hardcoding credentials in source code

Hardcoding your Supabase URL and anon key in your source code makes it harder to manage different environments.

✅ Correct: Use environment variables

Use --dart-define or a .env file with flutter_dotenv to manage credentials.

🎯 Key Takeaway

Supabase is a powerful open-source Firebase alternative built on PostgreSQL. Setting up your Flutter app with Supabase is simple: create a project, get your API keys, and initialize the client. Always use the anon key for client-side code and store credentials securely using environment variables.