Reputation: 2598
Don't know how to get responce from cloud function in flutter.
My cloud function
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.testDemo = functions.https.onRequest((request, response) => {
return response.status(200).json({msg:"Hello from Firebase!"});
});
My flutter code
///Getting an instance of the callable function:
try {
final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
functionName: 'testDemo',);
///Calling the function with parameters:
dynamic resp = await callable.call();
print("this is responce from firebase $resp");
} on CloudFunctionsException catch (e) {
print('caught firebase functions exception');
print(e.code);
print(e.message);
print(e.details);
} catch (e) {
print('caught generic exception');
print(e);
}
flutter: caught firebase functions exception flutter: INTERNAL flutter: Response is missing data field. flutter: null
Upvotes: 14
Views: 9493
Reputation: 395
you can convert to .onCall type function if you want to call function by it's name. aldobaie's answer
or for onRequest type function:
we need to call it as RESTful APIs.
While deploying to firebase, we'll get functions URL
or we can get function URL from dashboard of functions from firebase.
URL is in formate of:
https://us-central1-< projectName >.cloudfunctions.net/< functionName >
call it as any other RESTful API. plus point of using it as API is that data field in response isn't mandatory and freedom of response formate.
Upvotes: 0
Reputation: 4106
You must explicity put the attribute "data" in the json of response.
Like:
response.send({
"status" : success,
"data" : "some... data"
});
Upvotes: 8
Reputation: 1407
Use
exports.testDemo = functions.https.onCall((data, context) => {
return {msg:"Hello from Firebase!"};
});
in cloud functions. Callable is different than Request
When you call the functions you need to add the parameters:
change:
// Calling a function without parameters is a different function!
dynamic resp = await callable.call();
to:
dynamic resp = await callable.call(
<String, dynamic>{
'YOUR_PARAMETER_NAME': 'YOUR_PARAMETER_VALUE',
},
);
as described here
then to print the response:
print(resp.data)
print(resp.data['msg'])
The Firebase Functions for flutter example here and here
Upvotes: 16