FlutterFlow / flutterflow-issues

A community issue tracker for FlutterFlow.
115 stars 19 forks source link

Unexplained error while creating a custom function 'contains' #3379

Closed VmarcellusR closed 1 month ago

VmarcellusR commented 1 month ago

Can we access your project?

Current Behavior

I keep getting new error while creating a custom function. I wanted to let you know what notifications I'm getting but they're constantly changing, while I don't change the code. So in short, the problem is that my custom function isn't getting approved.

Expected Behavior

I have a chat function in my app, and in order to not create multiple chats between the same users I decided to make action along the lines of:

"if a Chat document which field 'ChatMembers' includes authenticated user and the other user exists - navigate to page with this chat. If it doesn't exist yet, create a chat between those users and navigate to page with this chat"

But to implement this logic I need to create a custom function 'contains' to check if in 'Chat' collection has any document which contains these 2 particular users in its 'ChatMembers' field.

So 1 - it should work, and 2 - It should check for particular users provided by a variable.

Steps to Reproduce

Go to my app > custom functions > it's a function called 'check4Members' > and try to unerror/approve this function.

Right now in my app, there are 2 errors, both related to this thing

Reproducible from Blank

Bug Report Code (Required)

ITESl/Lfx5RgrsMD+s/AdMdAhQAkH1x/bZJEl9caRRUjfarkO5kqZOr+VxZvOd+7TW9LeVCajTkd+ujtkt7pCfAVCzOcaINo+bl2SQLKYH2iVLGYCsywR0YlL/9hGH3C0MC7nh55AMBoSVoew3z/cq3qNleeY8aSfxBlZ7vfcPo=

Visual documentation

cc

Environment

- FlutterFlow version: new one
- Platform: desktop
- Browser name and version: brave
- Operating system and version affected: windows 10

Additional Information

I generated the bug code on the button which triggers 'create chat' action.

Alezanello commented 1 month ago

Hello!

At first glance, without checking your project code in detail, it seems that the Custom Function you marked is supposed to return a boolean value. However, the function currently does not return anything, which could be causing one of the issues.

I'll take a deeper look into the project to see more details, but in the meantime, please try adding a return statement to the function.

Alezanello commented 1 month ago

In a new blank project, adding a return statement (true or false) as expected solved the issue.

Now, as a suggestion based on your needs, I recommend performing these types of comparisons or querying Firestore in a Custom Action rather than a Custom Function.

Here is a suggested code snippet:

// Automatic FlutterFlow imports
import '/backend/backend.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:cloud_firestore/cloud_firestore.dart';

Future<bool> check4Members(
  DocumentReference? currentUser,
  DocumentReference? otherUser,
) async {
  /// MODIFY CODE ONLY BELOW THIS LINE

  if (currentUser == null || otherUser == null) {
    return false;
  }

  // Query Firestore to find a chat document with both users in the ChatMembers field
  final querySnapshot = await FirebaseFirestore.instance
      .collection('Chat')
      .where('ChatMembers', arrayContains: currentUser)
      .get();

  // Check if any document contains both users
  for (var doc in querySnapshot.docs) {
    List<dynamic> chatMembers = doc['ChatMembers'];
    if (chatMembers.contains(currentUser) && chatMembers.contains(otherUser)) {
      return true;
    }
  }

  return false;

  /// MODIFY CODE ONLY ABOVE THIS LINE
}

Best regards,
Azanello

VmarcellusR commented 1 month ago

Hi, thank you very much! I'm a complete noob when it comes to custom functions/actions, could you confirm for me if that's how it should look like (I attached a screenshot). Once again thank you.

On Fri, Jul 12, 2024 at 5:19 PM Alezanello @.***> wrote:

In a new blank project, adding a return statement (true or false) as expected solved the issue.

Now, as a suggestion based on your needs, I recommend performing these types of comparisons or querying Firestore in a Custom Action rather than a Custom Function.

Here is a suggested code snippet:

// Automatic FlutterFlow importsimport '/backend/backend.dart';import '/flutter_flow/flutter_flow_theme.dart';import '/flutter_flow/flutter_flow_util.dart';import '/custom_code/actions/index.dart'; // Imports other custom actionsimport '/flutter_flow/custom_functions.dart'; // Imports custom functionsimport 'package:flutter/material.dart';// Begin custom action code// DO NOT REMOVE OR MODIFY THE CODE ABOVE! import 'package:cloud_firestore/cloud_firestore.dart'; Future check4Members( DocumentReference? currentUser, DocumentReference? otherUser, ) async { /// MODIFY CODE ONLY BELOW THIS LINE if (currentUser == null || otherUser == null) { return false; }

// Query Firestore to find a chat document with both users in the ChatMembers field final querySnapshot = await FirebaseFirestore.instance .collection('Chat') .where('ChatMembers', arrayContains: currentUser) .get();

// Check if any document contains both users for (var doc in querySnapshot.docs) { List chatMembers = doc['ChatMembers']; if (chatMembers.contains(currentUser) && chatMembers.contains(otherUser)) { return true; } }

return false;

/// MODIFY CODE ONLY ABOVE THIS LINE}

Best regards, Azanello

— Reply to this email directly, view it on GitHub https://github.com/FlutterFlow/flutterflow-issues/issues/3379#issuecomment-2225575734, or unsubscribe https://github.com/notifications/unsubscribe-auth/BFSOGU4QZUAU73ARTDGOJWTZL7JUZAVCNFSM6AAAAABKYMPOA2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRVGU3TKNZTGQ . You are receiving this because you authored the thread.Message ID: @.***>

github-actions[bot] commented 1 month ago

This issue is stale because it has been open for 7 days with no activity. If there are no further updates, a team member will close the issue.