0

I recently started developing an app using Flutter and Firebase. I use Firebase Emulator to test Authentication and Cloud Functions. Most of my code is in the Firebase Cloud Functions which I use for all CRUD for Firestore and RTDB. While adding some new features, I got this error in my app. I tried searching a lot but could not find any solution. The following is the error is receive:

An error occured while calling function profile-get
Error Details: null
Message: An internal error has occurred, print and inspect the error details for more information.
Plugin: firebase_functions
Stacktrace: null

My API class in Flutter:

class Api {

  Api(this.functions);
  final FirebaseFunctions functions;

  static Api init() {
    FirebaseFunctions functions = FirebaseFunctions.instance;
    if (emulator) functions.useFunctionsEmulator(origin: host);
    return Api(functions);
  }

  Future<ApiResult> call(String name, {
    Map<String, dynamic> parameters,
  }) async {
    try {
      HttpsCallable callable = functions.httpsCallable(name);
      HttpsCallableResult results = await callable.call(parameters);
      return ApiResult(new Map<String, dynamic>.from(results.data));
    } on FirebaseFunctionsException catch (e) {
      print('An error occurred while calling function $name.');
      print('Error Details: ${e.details}\nMessage: ${e.message}\nPlugin: ${e.plugin}\nStacktrace: ${e.stackTrace}');
      return ApiResult({
        'status': 'error',
        'message': 'An error occured',
        'code': 'unknown'
      });
    }
  }

  static String get host => Platform.isAndroid ? 'http://10.0.2.2:2021' : 'http://localhost:2021';

}

I tried running the functions directly from their local URL and they work fine.

4
  • Can you share the code of the functions with us? Can you confirm that the function is called at all? For me it looks like the error is in the function itself. Commented Apr 23, 2021 at 9:58
  • The problem is in the Client side. Even the simplest functions don't work. I have tried calling the functions using their URLs and they work fine. But I'm unable to track the error. Commented Apr 23, 2021 at 14:18
  • Does your function have a onCall or onRequest trigger? Those are not the same. If you can call it using an URL you can't call it in your App as a callable functions. Commented Apr 23, 2021 at 19:58
  • Functions have onRequest trigger Commented Apr 25, 2021 at 12:38

2 Answers 2

1

As mentioned in the comments defore you are reating a cloud function with onRequest. Those are not callable using an SDK but only trough https URL.

To create a callable function that you can call trough Firebase SDKs you would need to refactor your functions to use the onCall.

It should look something like this:

exports.yourFunctionName= functions.https.onCall((data, context) => {
  // receive the data
  const text = data.text;

  // return a response
  return {
    test:'test'
  }
});

Here you have more information how the callable functions work.

Sign up to request clarification or add additional context in comments.

Comments

0

Are you using a different region than the standard us-central1? This is often the case, so you need to change the region you are calling from

HttpsCallable callable = FirebaseFunctions.instanceFor(region:"your_region").httpsCallable(name);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.