Newbie help with custom function PLEASE.

So, I'm giving 2 arguments to this function to grab a document in 'cruises' that pulls the field (array) 'users_doc_ref' which are obviously type DocumentReference. I then need it to query each of the 'users' documents that are listed in the 'users_doc_ref' and filter then by the 2nd argument (searchGender) in the users' your_type' field.I'm sure this is addressed somewhere, but is it possible to return a list of Document References from that filtered list where async is required? I can do it in an action, but I need it in a function because the data is not loaded before the page finishes loading. (if that makes sense)In the action, it returns a white screen at first and won't display the results unless you click it which seems to refresh the page AFTER the data is finished loading. Here is my code that currently returns nothing.

Thank you in advance.

List<DocumentReference>? fnFetchUsers(

String cruiseId,

String searchGender,

) {

/// MODIFY CODE ONLY BELOW THIS LINE

Future<List<DocumentReference>> fetchUsers() async {

try {

// Fetch the specific document from the 'cruises' collection using the cruiseId

final cruiseDoc = await FirebaseFirestore.instance

.collection('cruises')

.doc(cruiseId)

.get();

if (!cruiseDoc.exists) {

return [];

}

// Extract the 'users_doc_ref' list from the fetched document

List<DocumentReference> usersDocRefList =

List<DocumentReference>.from(cruiseDoc['users_doc_ref']);

// Fetch all user documents concurrently

List<DocumentSnapshot> userDocs =

await Future.wait(usersDocRefList.map((docRef) => docRef.get()));

// Filter the user documents based on the conditions

List<DocumentReference> filteredReferences = [];

for (var userDoc in userDocs) {

if (userDoc['your_type'] == searchGender) {

filteredReferences.add(userDoc.reference);

}

}

return filteredReferences;

} catch (e) {

print("Error fetching and filtering user doc refs: $e");

return [];

}

}

fetchUsers(); // Calling the inner function

/// MODIFY CODE ONLY ABOVE THIS LINE

1
5 replies