DEV Community

Cover image for Supabase in Flutter: Full CRUD, Authentication, and Riverpod State Management
Yadnesh Teli
Yadnesh Teli

Posted on AI-assisted

Supabase in Flutter: Full CRUD, Authentication, and Riverpod State Management

Series 5 — Part 1 of 1

Supabase in Flutter: Full CRUD, Authentication, and Riverpod State Management

A standalone post on integrating Supabase as an open-source Firebase alternative

After working with Firebase throughout the shopping app series, I was asked to explore Supabase — an open-source Firebase alternative built on PostgreSQL. The goal was to build a Flutter app with full CRUD, Supabase Auth, and Riverpod for state management.

Setting up Supabase

I created a Supabase project in the dashboard, generated the project URL and anon key, and added the supabase_flutter package to pubspec.yaml. Initialisation goes in main():

await Supabase.initialize(
  url: supabaseUrl,
  anonKey: supabaseKey,
);
Enter fullscreen mode Exit fullscreen mode

The Supabase client is then accessible globally via Supabase.instance.client.

Full CRUD implementation

Supabase exposes a Dart client that mirrors the PostgREST query builder. CRUD operations felt familiar:

// Create
await supabase.from('products').insert(product.toJson());

// Read
final data = await supabase.from('products').select();

// Update
await supabase.from('products').update({'name': name}).eq('id', id);

// Delete
await supabase.from('products').delete().eq('id', id);
Enter fullscreen mode Exit fullscreen mode

One thing to note: Supabase uses Row Level Security (RLS) by default. If you forget to set up RLS policies, your queries will silently return empty results — not an error! I spent time debugging this until I found the missing policy.

Supabase Auth with multiple providers

I implemented email/password sign-in and read the docs for OAuth providers. Supabase Auth is simpler to configure than Firebase for some providers — the dashboard makes OAuth setup straightforward. The Riverpod state management for auth followed the same pattern as the shopping app: an AuthRepository wrapping the Supabase auth client, and an AuthViewModel exposing the current user state.

Comparing Supabase with Firebase

Both are good, but they have different strengths:

  • Supabase gives you a real PostgreSQL database with SQL queries, relations, and foreign keys — useful when your data model is relational.
  • Firebase Firestore is a document database — easier for flexible, nested data structures.

If your team knows SQL, Supabase is immediately productive. If your team is mobile-first and has no SQL background, Firebase feels more natural.

Bugs I fixed

  • Cache Invalidation: I hit a state management bug where Riverpod state was not updating after a successful insert. The fix was to call ref.invalidate() on the products provider after the mutation, rather than manually updating the local state list.
  • Duplicate Auth Listeners: I also fixed a bug where the auth state listener was firing multiple times on startup — caused by subscribing to onAuthStateChange in initState without cancelling the subscription on dispose.

What I learned

  • RLS is mandatory: Supabase RLS policies are required — silent empty results without them are harder to debug than errors.
  • Fluent queries: The Supabase Dart client feels natural for anyone who has used a query builder before.
  • Use ref.invalidate(): Use ref.invalidate() to refresh Riverpod providers after mutations instead of manually patching state.
  • Relational power: Supabase is a strong choice when your data is relational and your team knows SQL.

Top comments (0)