I want to get a single row from supabase in Custom Action, but I get an error.

Custom Code

I'm developing a Flutter application that utilizes Supabase to fetch data from the promotion table based on user information. The goal is to retrieve a single promotion entry that matches specific criteria:
I would like to know how to get a single row from a supabase in Custom Action.
Thank you in advance.

  • target_genders includes userGender

  • target_grades includes userGrade

  • answered_users does not include userID

  • Limit the result to 1 entry

Here is the relevant portion of my current code:

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/backend/schema/enums/enums.dart';
import '/backend/supabase/supabase.dart';
import '/actions/actions.dart' as action_blocks;
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!

// Custom action code begins

Future<PromotionRow?> getZconnection(
  String? userID,
  String? userGrade,
  String? userGender,
) async {
  // Obtain Supabase client
  final supabase = SupaFlow.client;

  // Execute query
  final response = await supabase
      .from('promotion') // Table name
      .select()
      .contains('target_genders',
          [userGender]) // Includes userGender in target_genders
      .contains(
          'target_grades', [userGrade]) // Includes userGrade in target_grades
      .not('answered_users', 'cs',
          '{"$userID"}') // Excludes userID from answered_users
      .limit(1)
      .maybeSingle(); // Replaced execute() with maybeSingle()

  // Check if response or data is null
  if (response == null || response.data == null) {
    return null;
  }

  // Convert data to PromotionRow and return
  return PromotionRow(response.data!);
}

current error:

The getter 'data' isn't defined for the type 'PostgrestMap'. Try importing the library that defines 'data', correcting the name to the name of an existing getter, or defining a getter or field named 'data'.
The getter 'data' isn't defined for the type 'PostgrestMap'. Try importing the library that defines 'data', correcting the name to the name of an existing getter, or defining a getter or field named 'data'.
What have you tried so far?
  • Initial Error with execute():

    • Error Message

      The method 'execute' isn't defined for the type 'PostgrestTransformBuilder'. Try correcting the name to the name of an existing method, or defining a method named 'execute'.
    • Solution Attempted: Replaced .execute() with .maybeSingle() to align with the updated Supabase Dart client methods.

  • Null Safety Error when Accessing data:

    • Error Message:

      The property 'data' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!').
    • Solution Attempted: Added null checks for response and response.data before accessing data. Later removed unnecessary null checks, which led to new errors.

  • Type Argument Error with maybeSingle():

    • Error Message:

      The method 'PostgrestTransformBuilder<PostgrestMap?> Function()' is declared with 0 type parameters, but 1 type arguments are given. Try adjusting the number of type arguments.
    • Solution Attempted: Specified a generic type in maybeSingle<Map<String, dynamic>>(), but encountered another error regarding the undefined data getter.

  • Undefined data Getter Error:

    • Error Message:

      The getter 'data' isn't defined for the type 'PostgrestMap'. Try importing the library that defines 'data', correcting the name to the name of an existing getter, or defining a getter or field named 'data'.
    • Solution Attempted: Attempted to access response.data! despite PostgrestMap not having a data property, leading to this error.

Did you check FlutterFlow's Documentation for this topic?
Yes
3
1 reply