Call a custom function from a custom action with Record as parameter.

Custom Code

Hello,

I am trying to create a custom function that will check a list inside a firestore document (given parameter). It works perfectly. But it is impossible for me to call it from a custom action...

It never works...

This is what I have :

Future<void> checkWidgetAccessNavigate(
  BuildContext context,
  String widgetToAccess, // Enum as String (e.g., "MyWidget")
  DocumentReference userRef,
  DocumentReference associationRef,
  String redirectToPage,
  String minimumAccessLevel, // "viewer" (level 1) or "editor" (level 2)
) async {
  try {
    // Fetch the association document
    final associationSnapshot = await associationRef.get();
    if (!associationSnapshot.exists) {
      throw Exception('Association document not found.');
    }

    // Explicitly cast the document data to the expected type
    final data = associationSnapshot.data() as Map<String, dynamic>?;

    if (data == null) {
      throw Exception('Invalid or missing data in association document.');
    }

    // Create an AssociationRecord from the data (if not already typed as such)
    final AssociationRecord associationData = AssociationRecord.fromMap(data);

    // Call checkWidgetAccessFunction to verify access
    final bool hasAccess = checkWidgetAccessFunction(
      widgetToAccess,
      userRef,
      associationData,
      minimumAccessLevel,
    );

    // Redirect or deny access based on the result
    if (hasAccess) {
      // Navigate to the specified page
      context.pushNamed(
        redirectToPage,
        queryParameters: {
          'accessAs': serializeParam(
            minimumAccessLevel,
            ParamType.String,
          ),
        }.withoutNulls,
      );
    } else {
      throw Exception(
          'Access denied: insufficient permissions for widget "$widgetToAccess".');
    }
  } catch (e) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Error: ${e.toString()}')),
    );
  }
}

It tells me fromMap is not defined.

What have you tried so far?

I tried replacing the line fromMap with :

final AssociationRecord associationData = AssociationRecord.fromSnapshot(associationSnapshot);

But still nothing.....

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