Home > Mobile >  How do I properly set up my flutter project to make calls to Cloud Functions?
How do I properly set up my flutter project to make calls to Cloud Functions?

Time:06-23

The setup

I started learning Flutter a few months ago and am now trying to integrate an existing Firebase project into a new Flutter project. I created the new app in the Firebase Console, and followed Google's setup instructions. I am importing the necessary libraries and am using the flutter default app as a playground for me. I have a button set up so that when you press it, it calls _doFirebaseStuff() like so:

Future<void> _doFirebaseStuff() async {
    HttpsCallable callable = FirebaseFunctions.instance.httpsCallable("loadNews");
    var data = await callable.call(); // This line causes the error
    print(data);
}

The problem

The expected behavior, of course, is to call the Cloud Function and return the data.

What ends up actually happening is the following error being thrown: FirebaseFunctionsException ([firebase_functions/1] The operation couldn’t be completed. Operation not permitted. I haven't been able to find anything in the Flutter or Firebase documentation, nor anything relevant in the source code around the stack trace.

Troubleshooting

I have suspicions that one of the following reasons can be at the root of this issue:

  • Improper configuration of Firebase (I don't have a Google-Services.plist file anywhere in my project)
  • AppCheck is screwing things up
    • I don't think this is the issue because:
      • I have tested the same setup with (what I think was) a correct implementation of AppCheck
      • I am not getting a Failed Precondition: The request must come from a verified app message
      • The function call is not showing up in the Cloud Function logs, so I don't think the request is even being made and then denied, it fails right away
  • Improper IAM permissions on the Cloud Console
    • I don't think this is the issue because:
      • I am successfully using these functions in a different app
      • The function call is not even showing up in the Console logs

However there could be other things that I'm not thinking of.

Environment

  • Language: Dart
  • Flutter Version: 3.0.2
  • Target platform: MacOS
  • pubspec.yaml data:
    • cloud_functions: ^3.2.17
    • firebase_core: ^1.18.0

I'm tearing my hair out trying to debug this and would really appreciate any help!

CodePudding user response:

Can you follow this and this ? These instructions will help you setup the Google-Services.plist file.

Also, if after setting up your app as per the instructions, if you still face errors, please surround your call to the function like this:

try {
  HttpsCallable callable = FirebaseFunctions.instance.httpsCallable("loadNews");
  final result =
      await callable.call();
  print(data);
} on FirebaseFunctionsException catch (error) {
  print(error.code);
  print(error.details);
  print(error.message);
}

And share what you get in the logs (The error code, details and message will be printed in the logs).

  • Related