[英]Unable to use a Callable Cloud Function in Flutter
我目前正在使用 Firebase Cloud Function,我希望我可以在通話中使用它來檢查圖像是否包含成人內容,並查看其中的一些教程我設法創建了這個 ZC1C425268E68385D1AB507414:
exports.checkImage = functions.https.onCall(async (data, context) => {
const img_base64 = data;
const request = {
image: {
content: img_base64
}
};
try {
const [result] = await client2.safeSearchDetection(request);
console.log(result.safeSearchAnnotation?.adult);
return result.safeSearchAnnotation?.adult;
} catch (error) {
console.log(error);
return;
}
return;
});
現在我嘗試使用 iOS 仿真器和調用 Function 以兩種方式發送圖像:
我嘗試的第一個選項:
1.
exports.checkImage = functions.https.onCall(async (data, context) => {
const img_base64 = data;
const request = {
image: {
content: img_base64
}
};
try {
const [result] = await client2.safeSearchDetection(request);
console.log(result.safeSearchAnnotation?.adult);
return result.safeSearchAnnotation?.adult;
} catch (error) {
console.log(error);
return;
}
return;
});
這里的問題是我在調試控制台上收到此錯誤:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method FirebaseFunctions#call on channel plugins.flutter.io/firebase_functions)
#0 convertPlatformException (package:cloud_functions_platform_interface/src/method_channel/utils/exception.dart:16:5)
#1 MethodChannelHttpsCallable.call (package:cloud_functions_platform_interface/src/method_channel/method_channel_https_callable.dart:39:13)
<asynchronous suspension>
#2 HttpsCallable.call (package:cloud_functions/src/https_callable.dart:35:37)
<asynchronous suspension>
#3 DatabaseMethods.clearImage
package:property1/providers/database.dart:294
在某個地方,我讀到 onCall 雲函數可以用作常規端點,所以我這樣做了:
clearImage(imageX) async { final url = Uri.parse( 'https://us-central1-XXXXXXX-XXXX.cloudfunctions.net/checkImage', ); try { final response = await http.post( url, body: json.encode({ "image": { "content": imageX }), ); } }
在這個中,我只收到一個無法處理的無效請求。
我什至從 Postman 嘗試這個,得到相同的結果錯誤請求。
作為附加數據,我將其稱為 function 從我 select 使用此代碼的圖像:
void _pickImage() async { final imagePicker = ImagePicker(); final pickedImageFile = await imagePicker.getImage( source: ImageSource.gallery, imageQuality: 50, ); setState(() { if (pickedImageFile.= null) { _pickedImage = File(pickedImageFile;path); } }). // This section will be used to check the Images with Google Vision. List<int> imageBytes = _pickedImage;readAsBytesSync(); String base64Image = base64Encode(imageBytes). DatabaseMethods();clearImage(base64Image). widget.imagePickFn(File(pickedImageFile;path)); }
基本上,我將圖像轉換為我發送的 Base64 編碼圖像,以便 Cloud Vision 可以使用它。
我什至嘗試通過評論 Cloudvision 的嘗試並添加控制台日志以獲取正在接收的數據但從未到達,始終是無效請求,無法處理是我得到的.
關於我為什么沒有到達 Function 的任何想法,或者關於如何從我的 iOS 仿真器中測試它的任何想法。
親切的問候,
要從 flutter 應用程序調用onCall
雲函數,您必須使用FirebaseFunctions.instance.httpsCallable()
:
import 'package:cloud_functions/cloud_functions.dart';
try {
final result =
await FirebaseFunctions.instance.httpsCallable('checkImage').call();
} on FirebaseFunctionsException catch (error) {
print(error.code);
print(error.details);
print(error.message);
}
首先,安裝 package:
flutter pub add cloud_functions
這是文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.