Skip to content

Instantly share code, notes, and snippets.

@fabiojansenbr
Created June 14, 2021 16:37
Show Gist options
  • Save fabiojansenbr/a2ec7ed016a30e08c5a84bcd10e18c07 to your computer and use it in GitHub Desktop.
Save fabiojansenbr/a2ec7ed016a30e08c5a84bcd10e18c07 to your computer and use it in GitHub Desktop.
id title description
with-flutter
Quickstart: Flutter
Learn how to use Supabase in your Flutter App.

import Tabs from '@theme/Tabs' import TabItem from '@theme/TabItem'

Intro

This example provides the steps to build a simple user management app (from scratch!) using Supabase and Flutter. It includes:

  • Supabase Auth: users can sign in and sign up with email and password.

By the end of this guide you'll have an app which allows users to register and login with supabase:

Supabase Login and Register with Flutter

Github

Whenever you get stuck at any point, take a look at this repo.

Project set up

Before we start building we're going to set up our Supabase account.

Create a project

  1. Go to app.supabase.io.
  2. Click on "New Project".
  3. Enter your project details.
  4. Wait for the new database to launch.

Disable email confirmations

To make the process easier, let's remove the email confirmation constraint on the supabase portal.

<Tabs defaultValue="UI" values={[ {label: 'UI', value: 'UI'}, ]}>

https://gist.github.com/48483dc580240ddebb06be9fad020f21

Disable Email Confirmation

Get the API Keys

Now that you've setup your Supabase account, you are ready login and register your users. We just need to get the URL and anon key from the API settings.

<Tabs defaultValue="UI" values={[ {label: 'UI', value: 'UI'} ]}>

https://gist.github.com/11cdb68378fb3d12315ef10ae9a7897e

Building the App

Let's start building the Flutter app from scratch.

Initialize a Flutter App

https://gist.github.com/a6b932e3d3092da6257af9831fe0de3b

Then open the supabase_flutter folder on your favorite editor.

Install some dependencies

Let's install some dependencies to help us:

https://gist.github.com/d354d65b6414a3ebf0d9562867458651

And check the pubspec.yaml file:

Folder structure

Creating our project folder/files structure:

Folder structure

Configure app routes and Supabase client on main.dart

Let's open the main.dart file to setup the Supabase client as Singleton instance and configure the app routes:

https://gist.github.com/499ec293022264d58b5c1f2d0f8bd6c5

On the main() method we are creating a singleton instance of SupabaseClient, using the supabase_url and supabase_anon_key to link this project with our Supabase account. and in the MyApp method we are defining the routes off our app.

Creating the register view

Open the register_view.dart file and place the following code:

https://gist.github.com/5c7fa28eeb3d2d21de3e64f491d2a5fe

In this code, we are defining tree TextFormField(), email, password and password confirm and a _register() method wich will be called on press of the Register button.

When the _register() method is called, an instance of SupabaseClient is called and the method .auth.signup is invoked with the e-mail and password fields provided.

If the result.data is different than null, we are redirected to the Login view and a Dialog is shown. If we have some problem on registration, like duplicated e-mail field, an error dialog is shown, with the register error message.

Register ok

Creating the login view

Open the login_view.dart file and place the following code:

https://gist.github.com/2e4ab8bfbcd6ee69da3a6aa8e4014914

In this code, we are defining two TextFormField(), email and password and a _login() method wich will be called on press of the Login button.

When the _login() method is called, an instance of SupabaseClient is called and the method .auth.signin is invoked with the e-mail and password fields provided.

If the result.data is different than null, we are using shared_preferences to set an String 'user' key with the value of the persistSessionString returned from the signin method. This key will be used on the SplashView (we will create this on the next steps) to check if the user is already logged in on the application and redirects user to the HomeView and renew the Supabase credentials (token) without need to login again.

Home View

This view is quite straightforward. There's only a Text() widget which shows the Logged User's Email and a Logout button.

https://gist.github.com/7f6afe40e12de468f07cac5828069f0a

Flutter Home

On the build() method we are getting the current logged in user with final currentUser = GetIt.instance<SupabaseClient>().auth.user(); and using the currentUser.email property to show the User e-mail on the screen.

The _logout() method is responsible to signOut the user from the Supabase server and clean the shared_preferences so the user will need to login again.

Splash View

As i mentioned before, the Splash View will be the entrypoint route ('/') of our app. It will check if the user already has a session on the app. If there's a session, the user will be redirected to the Home View and their credentials will be renewed. If the session is not present, the user will be redirected to the Login View.

https://gist.github.com/4bfdb89a6093437ad225679963ef94ef

At this stage you have a fully functional application!

**Some design patterns could have been used to make the code better, but they would increase the difficulty level of this getting started .

Next steps

  • Got a question? Ask here.
  • Sign in: app.supabase.io
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment