I am trying to call firebase cloud functions from my flutter mobile application. I am able to call those from firebase functions : shell or locally

Viewed 466

I am trying to call firebase cloud functions, from my flutter mobile application, initially I ran all the logic in Firestore logic in my mobile application and came to know that it's not secure to run those kind of stuff in client application. So, now I am trying to implement on the logic in firebase cloud functions, but, I am stuck at this point from more than 3 to 4 days, also please comment correct way to call a callable firebase function with parameters. I tried by passing region to the cloud function and without passing region to the cloud functions, but both the methods failed. please help me figure this out, or leave any suggestions in comments below, any help is appreciated and thanks in advance.

The following are the codes I used in functions and flutter project :

My cloud functions (using Oncall) :

    import * as functions from "firebase-functions";
    import { usersRef } from "./config/firebase";
    
    const updateUserDataInDb = functions
      .region("asia-south1")
      .https.onCall((data, context) => {
        console.log(data);
        const userId = data.userId;
        const gender = data.gender;
        const phoneNumber = data.phoneNumber;
        const currentTime = Date.now();
        usersRef.doc(userId).update({
            gender: gender,
            phoneNumber: phoneNumber,
            deviceId: "FCM",
          });
          return {
            code: 200,
            message: "success",
          };
      });
    
    export { updateUserDataInDb };

my flutter code : I tried changing the code in multiple ways as people suggested in stack overflow and GitHub on other question's, but nothing worked in my case. Please help me overcome this issue, Thanks in advance.

 method 1 :
 updateuserdata() async {
    print('invoking function');
    var params = {
      "userId": "1234",
      "gender": "male",
      "phoneNumber": "9999999999",
    };
    try {
      HttpsCallable callable =
          FirebaseFunctions.instanceFor(region: "asia-south1").httpsCallable(
              'updateUserDataInDb',
              options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
      dynamic result = callable.call(params).catchError((onError) {
        print('function failed');
      });
      print(result);
    } catch (error) {
      print(error);
    }
  }
  method 2 : 
  updateuserdata() async {
    print('invoking function');
    var params = {
      "userId": "1234",
      "gender": "male",
      "phoneNumber": "9999999999",
    };
    try {
      HttpsCallable callable =
          FirebaseFunctions.instanceFor(region: "asia-south1").httpsCallable(
              'updateUserDataInDb',
              options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
      dynamic result = callable(params);
      print(result);
    } catch (error) {
      print(error);
    }
  }
  method 3 : 
  updateuserdata() async {
    print('invoking function');
    var params = {
      "userId": "1234",
      "gender": "male",
      "phoneNumber": "9999999999",
    };
    try {
      HttpsCallable callable =
          FirebaseFunctions.instance.httpsCallable(
              'updateUserDataInDb',
              options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
      dynamic result = callable.call(params);
      print(result);
    } catch (error) {
      print(error);
    }
  }
  method 4:
  updateuserdata() async {
    print('invoking function');
    var params = {
      "userId": "1234",
      "gender": "male",
      "phoneNumber": "9999999999",
    };
    try {
      HttpsCallable callable =
          FirebaseFunctions.instanceFor(app : Firebase.app(), region: "asia-south1").httpsCallable(
              'updateUserDataInDb',
              options: HttpsCallableOptions(timeout: Duration(seconds: 5)));
      dynamic result = callable.call(params).catchError((onError) {
        print('function failed');
      });
      print(result);
    } catch (error) {
      print(error);
    }
  }

Errors I am getting :

Error 1 :
PlatformException (PlatformException(3840, The data couldn’t be read because it isn’t in the correct format., {message: The data couldn’t be read because it isn’t in the correct format., code: 3840}, null))

Error 2 :
Exception has occurred. PlatformException (PlatformException(not-found, NOT FOUND, {message: NOT FOUND, code: not-found}, null))

Error 3:
Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.

Error 4:
throw PlatformException(code: errorCode, message: errorMessage as String?, details: errorDetails, stacktrace: errorStacktrace);

2 Answers

You are defining params but never send it to your cloud function so the cloud function will wail internaly because it dan't find the paths it expects.

It looks like you're not awaiting your call() functions:

dynamic result = await callable.call(params)
    .catchError((onError) {
        print('function failed');
    });
Related