I need my screens to update automatically whenever there is a data update in my Supabase database. I currently use custom actions to refresh my list views.
With the new Supabase v2 update and its Streaming Queries feature, tables now update automatically without needing my custom functions.
However, some of my widgets use supbase views as data sources, and since views do not have a primary key, the new streaming function doesn’t work with them.
How can I update my custom actions, or is there another way to achieve this?
// Automatic FlutterFlow imports
import '/backend/supabase/supabase.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'package:supabase_flutter/supabase_flutter.dart';
/// Subscribes to a specified table in Supabase to receive real-time updates.
///
/// [table]: The name of the table to monitor in the Supabase database.
/// [callbackAction]: A callback function to execute when a change occurs in the table.
Future<void> subscribe(String table, Future Function() callbackAction) async {
// Accessing the Supabase client and subscribing to a specific channel.
// The channel corresponds to the specified table, indicated by 'public:$table'.
// This is where we listen for real-time updates from the table.
SupaFlow.client
.channel('public:$table')
// Setting up an event listener on the channel.
// It listens for any ('*') Postgres changes (inserts, updates, deletes, etc.),
// specifically on the 'public' schema of the given table.
.on(
RealtimeListenTypes.postgresChanges,
ChannelFilter(event: "*", schema: 'public', table: table),
// Defining the action to take when an update is detected.
// The function 'callbackAction' is called in response to these changes.
(payload, [ref]) => callbackAction(),
)
// Finalizing the subscription to start listening for the events.
.subscribe();