Lab
Lab

Reputation: 1407

Flutter: Can't reach Firebase cloud functions emulator, a server with the specified hostname could not be found

Although it is possible for me to call a Cloud function that has been deployed, using the Firebase emulator returns an error.

Error

 A server with the specified hostname could not be found

Flutter code

class CloudFunctionHandler {
  static Future<HttpsCallableResult> callCloudFunction({
    required String functionName,
    required Map<String, dynamic> parameters,
    bool isLocal = false,
  }) async {
    HttpsCallable callable;
    if (isLocal) {
      // FirebaseFunctions.instanceFor(
      //         region: "europe-west3") // NOTE Already try this
      //     .useFunctionsEmulator('http://127.0.0.1', 5001);
      FirebaseFunctions.instance.useFunctionsEmulator('http://127.0.0.1', 5001);

      callable = FirebaseFunctions.instance
          .httpsCallable(functionName, options: HttpsCallableOptions());
    } else {
      callable = FirebaseFunctions.instanceFor(region: "europe-west3")
          .httpsCallable(functionName, options: HttpsCallableOptions());
    }

    try {
      HttpsCallableResult result = await callable.call(parameters);
      return result;
    } on FirebaseFunctionsException catch (e) {
      print(e.message);
      throw CustomException(
        message: e.message,
        code: e.code,
      );
    } catch (e) {
      if (e is PlatformException) {
        final error = e as PlatformException;

        throw CustomException(
          message: error.details["message"] as String,
          code: error.details["code"] as String,
        );
      } else {
        throw CustomException(
          message: "Un erreur est survenue",
          code: e.toString(),
        );
      }
    }
  }
}

Firebase.json


{
  "functions": {},
  "emulators": {
    "functions": {
      "port": 5001
    },
    "firestore": {
      "port": 8080
    },
    "ui": {
      "enabled": true
    }
  }
}

Would it come from the url? Also to know that I test on IOS simulator, and I have already added NSAllowsLocalNetworking and NSAllowsArbitraryLoads in info.plist

Upvotes: 0

Views: 844

Answers (1)

Lab
Lab

Reputation: 1407

The reason of the error was:

exports.create = functions.https
  .region("europe-west3") <--- This line
  .onCall(async (data, context) => {
    userProfilDatabase.create(data, context.auth.uid);
  });

I never had this problem in my previous projects, so I found a temporary solution to be able to test with the emulator. I comment .region("europe-west3") locally on emulator so that the connection is established. This may be related to the fact that I am now using the latest version of packacge cloud_functions (^ 3.0.4), as no problem with previous versions.

This is of course a temporary solution, I will be happy to test and validate any other proposals that would be more permanent.

Upvotes: 1

Related Questions