繁体   English   中英

无法在 Flutter 中使用可调用云 Function

[英]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
  1. 在某个地方,我读到 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM