【发布时间】: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