Flutter Firebase Cloud功能无法调用 [英] Flutter Firebase Cloud function can not be called
问题描述
当我尝试从Flutter调用可调用函数时,使用Firebase Cloud Functions时出现错误.
I am getting an error whilst using Firebase Cloud Functions when I try to call a callable function from Flutter.
flutter: caught generic exception
flutter: PlatformException(functionsError, Firebase function failed with exception., {message: NOT FOUND, code: NOT_FOUND})
这是我尝试使用cloud_functions调用云函数的方法:^ 0.4.2 + 3
Here is how I try to call the cloud function with using cloud_functions: ^0.4.2+3
import 'package:cloud_functions/cloud_functions.dart';
_check(String id) async {
HttpsCallable callable = CloudFunctions.instance
.getHttpsCallable(functionName: 'checkUserFavorites');
try {
final HttpsCallableResult result = await callable.call(
<String, dynamic>{
'id': id,
},
);
print(result.data);
} on CloudFunctionsException catch (e) {
print('caught firebase functions exception');
print(e.code);
print(e.message);
print(e.details);
} catch (e) {
print('caught generic exception');
print(e);
}
}
推荐答案
I have experienced similar issues, and with few days of debugging and experimenting I found the solution only after studying the source code of Cloud Functions Plugin for Flutter.
部署Firebase Cloud功能时,可以选择任何首选区域(越接近您的应用程序越好).例如
When you deploy Firebase Cloud function, you can choose any region of preference (closer to your application the better). For example
// using DigitalOcean spaces
exports.generateCloudImageUrl = functions
.region('europe-west3')
.https.onCall((reqData, context) => {
...
}
当您想从Flutter应用程序中调用此函数时,必须指定区域,否则全部进入us-central1
,这是默认设置.请参阅示例代码,了解如何使用在特定区域中部署的功能
When you want to call this function from Flutter app, you must specify the region, otherwise all goes to us-central1
which is default. See example code on how to use a function deployed in a specific region
final HttpsCallable generateCloudImageUrl = new CloudFunctions(region: "europe-west3")
.getHttpsCallable(functionName: 'generateCloudImageUrl');
// NB! if you initialize with 'CloudFunctions.instance' then this uses 'us-central1' as default region!
请参见 cloud_function源用于初始化.
这篇关于Flutter Firebase Cloud功能无法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!