【问题标题】:Flutter: Can't reach Firebase cloud functions emulator, a server with the specified hostname could not be foundFlutter:无法访问 Firebase 云功能模拟器,找不到具有指定主机名的服务器
【发布时间】:2021-12-02 17:06:38
【问题描述】:

虽然我可以调用已部署的 Cloud 函数,但使用 Firebase 模拟器会返回错误。

错误

 A server with the specified hostname could not be found

颤振代码

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
    }
  }
}

它会来自网址吗?另外要知道我是在IOS模拟器上测试的,而且我已经在info.plist中添加了NSAllowsLocalNetworking和NSAllowsArbitraryLoads

【问题讨论】:

  • 它是否适用于FirebaseFunctions.instance.useFunctionsEmulator('localhost', 5001);
  • @FaridShumbar 也不工作,同样的错误
  • 函数模拟器是否正在运行,您可以连接到模拟器 UI 并看到函数模拟器处于活动状态吗?
  • 是的,模拟器处于活动状态并照常运行(处理其他项目)

标签: firebase flutter google-cloud-functions firebase-tools


【解决方案1】:

错误原因是:

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

我在之前的项目中从来没有遇到过这个问题,所以我找到了一个临时解决方案,可以用模拟器进行测试。我在模拟器上本地评论.region("europe-west3"),以便建立连接。这可能与我现在使用的是最新版本的packacge cloud_functions(^ 3.0.4)有关,因为之前的版本没有问题。

这当然是一个临时解决方案,我很乐意测试和验证任何其他更永久的提案。

【讨论】:

    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多