Links

FlutterFlow + PowerSync

Integration guide for creating offline-first apps with FlutterFlow and PowerSync with Supabase as the backend, using FlutterFlow's Supabase tutorial as a starting point.
Before you begin, it's important that you (or someone on your team) are comfortable with FlutterFlow's workflow for managing custom code in GitHub as documented here. Using PowerSync with FlutterFlow currently requires dropping down into Dart code and merging code changes using git.
Skills needed to use this guide:
  • FlutterFlow
  • GitHub
  • Dart & Flutter
  • SQL
Used in conjunction with FlutterFlow, PowerSync enables developers to build offline-first apps that are robust in poor network conditions and that have highly responsive frontends while relying on Supabase for their backend. This guide provides instructions for how to configure PowerSync for use with your FlutterFlow project that has Supabase integration enabled.
This guide takes just over 1 hour to complete. The bulk of that time is the initial FlutterFlow tutorial.
Before you proceed, this guide assumes that you have already signed up for free accounts with both Supabase and PowerSync. If you haven't signed up for a PowerSync account yet, click here (and if you haven't signed up for Supabase yet, click here). This guide also assumes that you already have Flutter set up.

Guide Overview

  1. 1.
    Create your FlutterFlow project, integrated with Supabase
  2. 2.
    Connect your project to GitHub
  3. 3.
    Set up the PowerSync Service
  4. 4.
    Add PowerSync to Your Flutter Project
  5. 5.
    Lifecycle / Maintenance Considerations

Create Your FlutterFlow Project

This guide assumes you are starting off by following FlutterFlow's tutorial 'Build a Notes App With FlutterFlow and Supabase'. Some small changes are required to this tutorial to bring it up to date with the latest Supabase, and to make adding PowerSync simpler:
  1. 1.
    Create your notes table using the UUID type for your id column instead of int . It's simpler to work with UUID based primary key columns in PowerSync.
  2. 2.
    Disable Row Level Security (RLS) in Step 3 - Creating the notes table. Since the FlutterFlow tutorial was published, RLS is now mandatory for new tables created in Supabase. This is for development purposes only — it's important to use RLS in production.
  3. 3.
    Enable insert permissions for the Storage object in Step 4 - Creating a Storage Bucket. Without this, users won't be able to upload images.
    This can be found under Dashboard -> Storage -> Policies. This policy is a template provided by Supabase.
  4. 4.
    In Step 10 - Displaying Data in the UI, pass the id column as a parameter instead of the entire row

Connect Your Project to GitHub

  1. 1.
    Follow FlutterFlow's instructions published here: Manage Custom Code In GitHub
  2. 2.
    It's a good idea to make sure you can launch your app using flutter run at this point.

Set Up the PowerSync Service

  1. 1.
    In your Supabase project's SQL console, run the following:
CREATE PUBLICATION powersync FOR TABLE notes;
  1. 2.
    Connect PowerSync to your Supabase environment, as detailed in our Supabase Guide.
  2. 3.
    Deploy the below Sync Rules:
# Sync-rule docs: https://docs.powersync.com/usage/sync-rules
bucket_definitions:
global:
data:
# Sync all rows
- SELECT * FROM notes

PowerSync Flutter Integration

At a high level, the steps required in your Flutter code are as follows:
  1. 1.
    Install the PowerSync Dart package
  2. 2.
    Import helper functions
  3. 3.
    Define your app schema
  4. 4.
    Smoke test
  5. 5.
    Define your models
  6. 6.
    Connect your UI
All of the work in this section will be performed on the develop branch that you should have set up at this point.

Install PowerSync

  1. 1.
    Add the powersync Dart package to your pubspec.yaml file: powersync: ^0.4.1
    1. 1.
      It's also useful having a decent Logging framework in your app, so we also recommend adding logging: ^1.1.1
  2. 2.
    Run flutter pub get and iteratively resolve any dependency issues, for example updating flutter_cache_manager: 3.3.0 to flutter_cache_manager: ^3.3.0
    1. 1.
      See this commit to see exactly which dependencies we had to resolve.
Note: The FlutterFlow team are always updating their platform, so the exact dependencies that you will need to update may differ.

Import Helper Functions

  1. 1.
    Create a new filelib/powersync/powersync.dart and copy the contents of this file:
  1. 2.
    Replace the default value of powersyncUrl on line 12 with the value of your PowerSync Service Instance — you can obtain this by opening your Instance properties and copying the value of "Instance URL":
  2. 3.
    Make the following changes in lib/main.dart :
    1. 1.
      Add the following imports: import 'package:flutter/foundation.dart'; import './powersync/powersync.dart';
      import 'package:logging/logging.dart';
    2. 2.
      Add the below snippet and test query immediately before runApp(MyApp());:
await openDatabase();
waitForInitialSync();
try {
final test_query = await db.execute('SELECT * from notes');
log.info('test_query results = $test_query');
} on Exception catch (e) {
log.info('error querying notes table: $e');
}

Define App Schema

Import the following file into your project, it should be located in lib/powersync/models.schema.dart:

Smoke Test

At this point, you can launch your app using flutter run and you should see some 'notes' rows from your Supabase being printed out on the console! Something like this:
I/flutter (27958): [powersync-supabase] INFO: 2023-10-23 14:23:59.408889: test_query results = [{id: 4aeaabcd-4143-438d-a49b-903e051ec4bc, id:1: 4aeaabcd-4143-438d-a49b-903e051ec4bc, created_at: 2023-10-23 13:26:34.436Z, title: first note, note: success!, image_link: https://xyz.supabase.co/storage/v1/object/public/notes/users/uploads/1698089188788000.png}, {id: 56e99a0f-3e47-4d12-8fbe-5cb2b45da8ec, id:1: 56e99a0f-3e47-4d12-8fbe-5cb2b45da8ec, created_at: 2023-10-23 13:27:51.015Z, title: second note, note: workes :) , image_link: https://xyz.supabase.co/storage/v1/object/public/notes/users/uploads/1698089268369000.png}]

Define Your Models

FlutterFlow generates some models in backend/supabase/database/tables — we complement these by implementing our own offline-first models with static access methods.
Import the following 'notes' model into your project:

Connect Your UI

Generally speaking, you'll want to replace any Supabase calls in your UI to use the newly implemented methods from the appropriate models defined in the previous step.
In this app, we need to update three pages: home_list_page, note_display_page and create_note_page. It's recommended to do these one at a time.

Notes Display Page

Copy the changes from this commit into your notes display page.
This is a big milestone. At this point, force-quit your app, place your test device into airplane mode and relaunch your app. You should see your notes page being populated — all from your local SQLite database 🎉
Run your app and verify whether you can launch and see your notes list without connectivity (i.e. in offline mode).
Then repeat the steps for the note display and create note pages (link to commit).

Additional Resources

For more information, explore the PowerSync docs or join us on our community Discord where our team is always available to answer questions.