Hi,
Sorry for the length, I need to explain the use case in order to ask the question by the fact I have not found the right answer yet.
I have different building owners, each one has different buildings where they can rent office spaces for the full day, in the future I will think about hours but for the moment let me simplify it.
I have a table with the owners that contain of course the owner id.
Owners:
- id
- owner_name
- owner_unique_id
I have a table with the list of the buildings for each owner:
- id
- owner_unique_id
- building_unique_id
I have also a table where I record all the single room:
- id
- owner_unique_id
- building_unique_id
- room_unique_id
The last table is the booking table where to track the booking date:
- id
- owner_unique_id
- building_unique_id
- room_unique_id
- user_unique_id (who booked the room)
- booking_date
- book_flag (this could not be necessary for a full day but in the future could be for single hours)
I have already the calendar widget and I am creating the ListView below so that when a user select the date the list of the available rooms (based on the owner_unique_id and building_unique_id belonging to the user). Of course the list need to be refreshed when the user select the new date.
I want to maintain all the data in Supabase. Based on my little knowledge I can have two options, one is creating an Custom Action on the list view that return all the data, first listing the room table and for each one check if it is booked on that date, so the returned list is completed with the booking date available.
The second option is to create the ListView with a normal Backend query on the rooms table and then inside the listview for each row using a Custom Function looking inside the booking table to see if the room is available.
My first question is: which one is the best for you? I vote for the first so that I do not run one query for each room of the list, in terms of performance, but let me know.
My second question is about how to create a custom action/custom function for Supabase, I have tried with the internal chatgpt and the custom function looks like this:
import 'dart:convert';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/backend/supabase/supabase.dart';
import '/auth/supabase_auth/auth_util.dart';
String? getAvailability() {
/// MODIFY CODE ONLY BELOW THIS LINE
// write an async function to retrieve data from supabase table in input one string
Future<void> retrieveData(Datetime myDate) async {
// TODO: Implement function
}
/// MODIFY CODE ONLY ABOVE THIS LINE
}
then I asked chatgpt and I receive a couple of codes:
import 'package:supabase/supabase.dart';
class SupabaseService {
late SupabaseClient _supabaseClient;
SupabaseService(String supabaseUrl, String supabaseKey) {
_supabaseClient = SupabaseClient(supabaseUrl, supabaseKey);
}
Future<Map<String, dynamic>> readRecordWithInput(String tableName, String columnName, String inputString) async {
final response = await _supabaseClient
.from(tableName)
.select()
.eq(columnName, inputString)
.single()
.execute();
if (response.error != null) {
print('Error retrieving data: ${response.error!.message}');
return {'error': response.error!.message};
}
final Map<String, dynamic> data = response.data as Map<String, dynamic>;
return data;
}
}
void main() async {
// Example usage:
final supabaseService = SupabaseService('your_supabase_url', 'your_supabase_key');
String inputString = "example"; // Replace with your actual input string
final result = await supabaseService.readRecordWithInput('your_table_name', 'column_name', inputString);
if (result.containsKey('error')) {
print("Error: ${result['error']}");
} else {
print("Result: $result");
}
}
Now, I know that I need to copy and paste the part inside the void main() async {} BUT what about the class SupabaseService?
If I ask the internal AI for a custom Action I got this code that looks really nice ...
// 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!
Future getAvailability() async {
// write a custom action to read a supabase table and return a list for a listview widget
// Import the necessary packages
// import 'package:supabase/supabase.dart';
// Create a Supabase client instance
final supabaseClient = SupabaseClient('myurl', 'mykey');
// Define the table name
final tableName = 'availability';
// Query the table for all records
final response = await supabaseClient.from(tableName).select().execute();
// Check if the response was successful
if (response.error != null) {
// Handle the error
print(response.error.message);
return null;}
BUT ... it does not work, I got an error for "response.error" statements and I cannot save it at all
I have not found a real working example, if you can please support me let me know, also if you have different opinion and solution for this problem.
Thanks really a lot