Please lend me your wisdom.
The DataTable is taking a long time to load due to the large number of documents in the collection and also the large number of fields. So, to improve performance, I decided to create a custom function to retrieve only the data I need.
Currently, I am trying to narrow down the number of data to test it on a personal project.
I have finished writing the code and deployed the Custom Functions successfully, but the “Unable to process return parameter.Are you sure you want to save? Are you sure you want to save?”
The expected behavior is to load the FireStore collection, retrieve only the specified fields, and pass them to Page State via the Action Output Variable Name.
I am asking this question here because it is difficult to change the database in a production environment and I don't know anyone around here who is familiar with it.
I apologize if this is a rudimentary question, but I would appreciate if someone could help me.
Translation: Deepl
・Cloud Function
const functions = require('firebase-functions/v2'); //gen2 import
const admin = require('firebase-admin');
const db = admin.firestore();
exports.testUserList = functions.https.onCall(async (data, context) => {
try {
const snapshot = await db.collection("user").get(); //対象コレクションを全て取得
let items = [];
snapshot.forEach((doc) => {
const data = doc.data();
items.push({
email: data.email || "",
displayName: data.display_name || "",
organization: data.organization || "",
// In the production environment, there are also fields of type Boolean.
});
});
return { success: true, data: items };
} catch (error) {
console.error("Error fetching Firestore data:", error);
return { success: false, error: error.message };
}
});
・Custom Code
// 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_functions/cloud_functions.dart';
Future<List<Map<String, dynamic>>> testUserListFun() async {
final HttpsCallable callable = FirebaseFunctions.instance.httpsCallable(
'testUserList', // Firebase Functions v2 の関数名
);
try {
final HttpsCallableResult result = await callable.call();
if (result.data['success'] == true) {
return List<Map<String, dynamic>>.from(result.data['data']);
} else {
throw Exception("API Error: ${result.data['error']}");
}
} catch (e) {
print('Error fetching Firestore data: $e');
return [];
}
}