Sequence of selects/inserts in Supabase with Custom Action

Database & APIs
Resolved

Hello everyone, thank you in advance for the help.

I'm currently working on an app where I'm creating a "insert product" flow.
All the data is saved in 3 persisted AppStates of custom DataTypes, 2 of which are lists.
I'm trying to insert all this data in a Supabase database "at once", via the click of a single button, when the user finishes adding the product informations.
Unluckily I was not able to make this work properly, although I'm kind of there, I think.

Below is my current solution.
At first it returned an Authentication error that I fixed by getting the authenticated user.
As for the current issue, the data is added, but it just creates empty rows with the primary keys, and the foreign keys are correctly linked (that works at least!), but it doesn't add any other field:

Error creating product with details: NoSuchMethodError: method not found: 'data' Receiver: Instance of 'minified:aqG' 
Arguments: []

I already checked if I correctly pass the data from te Flutterflow frontend, as shown in the linked image

What have you tried so far?
//default imports
import 'package:supabase_flutter/supabase_flutter.dart';

// supabase initialization
final supabaseClient = Supabase.instance.client;

// get auth user
User? getAuthenticatedUser() {
  return supabaseClient.auth.currentUser;
}

Future handleProductInsert(
  String productName,
  List<StepStruct> steps,
  List<TimingsDataStruct> timings,
) async {
  try {
    // check if user is logged in
    final user = getAuthenticatedUser();
    if (user == null) {
      throw Exception('User is not authenticated');
    }

    // adding product data to db
    final productResponse = await supabaseClient
        .from('products')
        .insert({'product_name': productName}).select();

    if (productResponse.error != null || productResponse.data == null) {
      throw Exception('Failed to insert product');
    }

    final productId = productResponse.data[0]['product_id'];

    // save step ids
    List<int> stepIds = [];

    // adding steps data to db
    for (var step in steps) {
      final stepResponse = await supabaseClient.from('steps').insert({
        'product_id': productId,
        'step_name': step.stepName,
        'description': step.stepDesc,
        'avg_time': step.avgTime,
      }).select();

      if (stepResponse.error != null || stepResponse.data == null) {
        throw Exception('Failed to insert step');
      }

      final stepId = stepResponse.data[0]['step_id'];
      stepIds.add(stepId);
    }

    // add timings data to db
    for (int i = 0; i < stepIds.length; i++) {
      for (var timing in timings) {
        final timingResponse = await supabaseClient.from('timings').insert({
          'step_id': stepIds[i],
          'timing_value': timing.tempo,
        }).select();

        if (timingResponse.error != null || timingResponse.data == null) {
          throw Exception('Failed to insert timing');
        }
      }
    }
  } catch (error) {
    print('Error creating product with details: $error');
  }
}

(sorry for the long code, I couldn't make it shorter :(

Did you check FlutterFlow's Documentation for this topic?
Yes
1
7 replies