Search code examples
firebaseflutterdartgoogle-cloud-functions

Callable Cloud Function error: Response is missing data field


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


Solution

  • 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