Flutter Firebase函数:调用函数时出错



我最近开始使用Flutter和Firebase开发一个应用程序。我使用Firebase Emulator来测试身份验证和云功能。我的大部分代码都在Firebase Cloud函数中,我将其用于Firestore和RTDB的所有CRUD。在添加一些新功能时,我的应用程序中出现了此错误。我试着搜索了很多,但找不到任何解决方案。以下是接收到的错误:

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

我在Flutter的API课程:

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

我试着直接从它们的本地URL运行这些函数,它们运行得很好。

如注释所述,您正在使用onRequest创建一个云函数。这些不能使用SDK调用,只能通过https URL调用。

要创建一个可以通过Firebase SDK调用的可调用函数,您需要重构函数以使用onCall

它应该看起来像这样:

exports.yourFunctionName= functions.https.onCall((data, context) => {
// receive the data
const text = data.text;
// return a response
return {
test:'test'
}
});

这里有更多关于可调用函数如何工作的信息。

您使用的区域与标准的us-central1不同吗?这种情况经常发生,因此您需要更改您从呼叫的区域

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

最新更新