Update: I was able to find the source of the problem - it appears to be related to a circular reference in lodash which happens when serialising the response. Apparently I need to return a specific kind of promise, which I could not figure out how to do yet.
End of Update.
I am trying to get the official cloud function example from pub to work with dart using the packagefirebase_functions_interop which converts dart code to a deployable node based cloud function. I followed this tutorial to setup the build runner etc (and their example is working for me).
Now on to the callable functions which are what this question is all about.
This is a working example using JavaScript:
// JavaScript
const functions = require('firebase-functions');
exports.listFruit = functions.https.onCall(() => {
return [
"Apple",
"Banana",
"Cherry",
"Date",
"Fig",
"Grapes"
];
});
This is what I came up with using dart:
// Dart
import 'package:firebase_functions_interop/firebase_functions_interop.dart';
void main() {
functions['listFruit'] = functions.https.onCall((data, context) => listFruit);
}
FutureOr<List<String>> listFruit(dynamic data, CallableContext context) {
return ['Apple', 'Banana', 'Cherry', 'Date', 'Fig', 'Grapes'];
}
But upon execution I get this console output and a 500 error:
i functions: Beginning execution of "listFruit"
> Response cannot be encoded. Closure 'minified:ns' dh {
> '$initialize': [Function: dh],
> constructor: [Function: static_tear_off],
> '$static_name': 'ns',
> '$S': [Function (anonymous)],
> '$2': [Function: ns] {
> '$callName': '$2',
> '$R': 2,
> '$D': null,
> '$stubName': 'ns',
> '$tearOff': [Function (anonymous)]
> },
> '$C': [Function: ns] {
> '$callName': '$2',
> '$R': 2,
> '$D': null,
> '$stubName': 'ns',
> '$tearOff': [Function (anonymous)]
> },
> '$R': 2,
> '$D': null
> }
> {"severity":"ERROR","message":"Unhandled error Error: Invalid response, check logs for details
at Object.d (/test_dart_functions/functions/build/index.dart.js:183:3)
at h3.$2 (/test_dart_functions/functions/build/index.dart.js:4066:9)
at Object.m4 (/test_dart_functions/functions/build/index.dart.js:138:112)
at mE (/test_dart_functions/functions/build/index.dart.js:1872:10)
at /test_dart_functions/functions/build/index.dart.js:1866:42
at newHandler (/usr/local/Cellar/firebase-cli/9.1.0/libexec/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:286:16)
at func (/test_dart_functions/functions/node_modules/firebase-functions/lib/providers/https.js:273:32)
at /test_dart_functions/functions/node_modules/firebase-functions/lib/providers/https.js:293:44
at cors (/test_dart_functions/functions/node_modules/cors/lib/index.js:188:7)
at /test_dart_functions/functions/node_modules/cors/lib/index.js:224:17 {
dartException: HttpsError: Invalid response, check logs for details
at Object.n9 (/test_dart_functions/functions/build/index.dart.js:1825:15)
at bD.aO (/test_dart_functions/functions/build/index.dart.js:4023:24)
at h3.$2 (/test_dart_functions/functions/build/index.dart.js:4066:13)
at Object.m4 (/test_dart_functions/functions/build/index.dart.js:138:112)
at mE (/test_dart_functions/functions/build/index.dart.js:1872:10)
at /test_dart_functions/functions/build/index.dart.js:1866:42
at newHandler (/usr/local/Cellar/firebase-cli/9.1.0/libexec/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:286:16)
at func (/test_dart_functions/functions/node_modules/firebase-functions/lib/providers/https.js:273:32)
at /test_dart_functions/functions/node_modules/firebase-functions/lib/providers/https.js:293:44
at cors (/test_dart_functions/functions/node_modules/cors/lib/index.js:188:7) {
code: 'internal',
details: "Closure 'minified:ns'",
httpErrorCode: { canonicalName: 'INTERNAL', status: 500 }
}
}"}
i functions: Finished "listFruit" in ~1s
I'm really looking forward to writing all my cloud functions in dart - except for onCall the others seem to be working OK.
Any help would be greatly appreciated!
question from:
https://stackoverflow.com/questions/65890791/firebase-functions-oncall-not-working-in-dart-with-firebase-functions-interop