Supabase

How to use Supabase with Flutter?

Discover a clear guide on integrating Supabase with your Flutter app. Covering everything from setup to authentication and database operations, ensuring a seamless backend experience.

Developer profile skeleton
a developer thinking

Overview

Supabase, an alternative to Firebase, is open-source and offers backend services. It has a real-time database and user authentication that integrates smoothly with Flutter. To use Supabase with Flutter, set up a project in Supabase and configure the dependencies in the Flutter app. Next, implement Supabase's API for handling data and auth. This enables developers to tap into Supabase’s strengths while crafting cross-platform apps with Flutter. Basic Flutter know-how and some understanding of backend services will be needed.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to use Supabase with Flutter?

Step 1: Create a Supabase Account and Project

  • Head over to Supabase and sign up for an account.
  • Once you're in, create a new project from the Supabase dashboard.
  • Jot down the project URL and the API key from the project settings; you'll need these for the Flutter app.

Step 2: Set Up Flutter Application

  • Make sure Flutter is installed and ready to go on your machine.
  • Start a new Flutter project with this command:
    ```sh
    flutter create my_supabase_app
    ```
  • Move into the project directory:
    ```sh
    cd my_supabase_app
    ```

Step 3: Add Dependencies

  • Open up the pubspec.yaml file in your Flutter project.
  • Add these dependencies:
    ```yaml
    dependencies:
    flutter:
    sdk: flutter
    supabase: ^0.2.4
    ```
  • Run flutter pub get to pull in the dependencies.

Step 4: Initialize Supabase Client

  • Create a new Dart file, let's call it supabase_service.dart.

  • Set up the Supabase client with your project's URL and API key:
    ```dart
    import 'package:supabase/supabase.dart';

    class SupabaseService {
    final SupabaseClient client;

    SupabaseService(String supabaseUrl, String supabaseKey)
    : client = SupabaseClient(supabaseUrl, supabaseKey);
    // Additional methods to interact with Supabase
    }

    final supabaseService = SupabaseService('YOUR_SUPABASE_URL', 'YOUR_SUPABASE_KEY');
    ```

Step 5: Create Authentication Methods

  • Add methods for user authentication (sign-up and sign-in) in the supabase_service.dart file:
    ```dart
    class SupabaseService {
    final SupabaseClient client;

    SupabaseService(String supabaseUrl, String supabaseKey)
    : client = SupabaseClient(supabaseUrl, supabaseKey);
    Future signUp(String email, String password) async {
    final response = await client.auth.signUp(email, password);
    return response;
    }

    Future signIn(String email, String password) async {
    final response = await client.auth.signIn(email: email, password: password);
    return response;
    }
    }

    final supabaseService = SupabaseService('YOUR_SUPABASE_URL', 'YOUR_SUPABASE_KEY');
    ```

Step 6: Integrate Supabase with Flutter Widgets

  • Create a simple Flutter UI to let users sign up and sign in:
    ```dart
    import 'package:flutter/material.dart';

    void main() {
    runApp(MyApp());
    }

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    title: 'Supabase with Flutter',
    theme: ThemeData(
    primarySwatch: Colors.blue,
    ),
    home: SignUpPage(),
    );
    }
    ```

  • Create SignUpPage and SignInPage widgets and use the methods from the SupabaseService:

    ```dart
    class SignUpPage extends StatefulWidget {
    @override
    _SignUpPageState createState() => _SignUpPageState();
    }

    class _SignUpPageState extends State {
    final _emailController = TextEditingController();
    final _passwordController = TextEditingController();

    void _signUp() async {
    final response = await supabaseService.signUp(
    _emailController.text,
    _passwordController.text,
    );

    if (response.error == null) {
    // Handle user signed up successfully
    } else {
    // Handle error
    }
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: Text('Sign Up'),
    ),
    body: Padding(
    padding: const EdgeInsets.all(16.0),
    child: Column(
    children: [
    TextField(
    controller: _emailController,
    decoration: InputDecoration(labelText: 'Email'),
    ),
    TextField(
    controller: _passwordController,
    decoration: InputDecoration(labelText: 'Password'),
    obscureText: true,
    ),
    ElevatedButton(
    onPressed: _signUp,
    child: Text('Sign Up'),
    ),
    TextButton(
    onPressed: () {
    Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => SignInPage()),
    );
    },
    child: Text('Already have an account? Sign In'),
    ),
    ],
    ),
    ),
    );
    }
    }

    class SignInPage extends StatefulWidget {
    @override
    _SignInPageState createState() => _SignInPageState();
    }

    class _SignInPageState extends State {
    final _emailController = TextEditingController();
    final _passwordController = TextEditingController();

    void _signIn() async {
    final response = await supabaseService.signIn(
    _emailController.text,
    _passwordController.text,
    );

    if (response.error == null) {
    // Handle user signed in successfully
    } else {
    // Handle error
    }
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: Text('Sign In'),
    ),
    body: Padding(
    padding: const EdgeInsets.all(16.0),
    child: Column(
    children: [
    TextField(
    controller: _emailController,
    decoration: InputDecoration(labelText: 'Email'),
    ),
    TextField(
    controller: _passwordController,
    decoration: InputDecoration(labelText: 'Password'),
    obscureText: true,
    ),
    ElevatedButton(
    onPressed: _signIn,
    child: Text('Sign In'),
    ),
    ],
    ),
    ),
    );
    }
    }
    ```

  • Make sure the Navigator is used effectively to switch between SignUpPage and SignInPage.

Step 7: Run the Application

  • Fire up the Flutter application using the command:
    ```sh
    flutter run
    ```
  • Test the sign-up and sign-in functionality to make sure the Supabase integration is working smoothly with your Flutter app.

Explore more Supabase tutorials

Complete Guide to Supabase: Tutorials, Tips, and Best Practices

Explore our Supabase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.