I tried to call my cloud function using the cloud_functions plugin from my Flutter project with the following code:
final HttpsCallable callable = new CloudFunctions(region: "europe-west3")
.getHttpsCallable(functionName: 'helloWorld');
dynamic resp = await callable.call(<String, dynamic>{'id': id, 'chatId': chat.chatId});
And get the following error:
ERROR: PlatformException(3840, The data couldn’t be read because it isn’t in the correct format., null)
By my research, I saw that the problem can appear when you forget to put the region on the server and client side, but the error persist.
Also I try to pass by http request who succeed:
var parameters = {'id': id, 'chatId': chat.chatId};
var url = "https://europe-west3-{MY_DOMAIN}.cloudfunctions.net/helloWorld";
await http.post(url, body: parameters).then((res) {...}
So I think the problem come from the plugin where I maybe may have forgotten something. Any ideas ?
Cloud function (test):
exports.helloWorld = functions
.region('europe-west3')
.https.onRequest((request, response) => {
try {
response.send('Hello from Firebase!');
} catch (e) {
console.log(e);
throw new functions.https.HttpsError('calc-error', e);
}
});
As you are using a callable on your Flutter app, try to convert your function to use onCall instead of onRequest:
onCall:export const helloWorld = functions.https.onCall((data, context) => {
functions.logger.info("data:", data);
return {
message: "bye!"
};
});
emulator setup:
// after: Firebase.initializeApp()
FirebaseFunctions.instance.useFunctionsEmulator(origin: 'http://localhost:5001');
calling the function:
FirebaseFunctions functions = FirebaseFunctions.instance;
HttpsCallable callable = functions
.httpsCallable('helloWorld');
final results = await callable.call({
'name': 'Erick M. Sprengel',
'email': '[email protected]'
});
Be careful about the difference between onCall vs onRequest.
It's easy to convert, but I think it's better to check this question: Firebase Cloud Functions: Difference between onRequest and onCall
Extra tips:
npm run build -- --watchIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With