Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I call a callable gen2 Firebase Cloud Function in Flutter?

I’m trying to call my Firebase gen2 function from my Flutter app, however my cloud function can’t read the data sent to it nor pass it forward.

Here’s my Flutter function:

  Future<bool> _test() async {
    final httpCallable = FirebaseFunctions.instance
        .httpsCallableFromUrl(cloudFunctionsVerifyPurchase); // a valid url

    final data = <String, dynamic>{
      'dataKey1': 'dataValue1',
      'dataKey2': 'dataValue2',
    };

    final res = await httpCallable.call(data);
    print(res.data);
    return false;
  }

Here’s my cloud function:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

exports.test = onCall(async(data,context) => {
    // Access the data sent from the Flutter app
    const dataValue1 = data.dataKey1; // null
    const dataValue2 = data.dataKey2; // null
    
    // when I console.log(data) I get a massive object, and not the object I sent. I tried doing data.body but that also returns null
    
    return {
      message: dataValue1,
      result: dataValue2,
    };
});

My Cloud Function Logs (You can see that ‘dataKey1’ & ‘dataKey2’ exist!):

enter image description here

Flutter Function Output:
enter image description here

>Solution :

The signature of the onCall() method has changed with Cloud Functions 2nd gen: You now need to pass a unique request parameter which "contains data passed from the client app as well as additional context like authentication state" as explained in the doc.

So you need to adapt your code as follows:

exports.test = onCall(async(request) => {
    // Access the data sent from the Flutter app
    const dataValue1 = resquest.data.dataKey1; // null
    const dataValue2 = request.data.dataKey2; // null
    //...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading