【问题标题】:How to use google cloud functions in flutter?Flutter中如何使用谷歌云功能?
【发布时间】:2020-10-14 17:21:26
【问题描述】:

现在这是 index.js 文件

import 'dart:math';

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

exports.getRandomNumbers=functions.https.onCall((num){

   const numbers=admin.firestore().collection('RandomNumbers').document('CurrentRandomNumber');
   return numbers.add({
   RandomNumber=new Random().nextInt(10);
   });

});

以及完成任务的功能

onPressed: (){
                CloudFunctions.instance.getHttpsCallable(

                    functionName: "getRandomNumbers",
                );
              },

现在你可以看到我正在尝试获取一个随机数并将其添加到 firestore 数据库,但它既不工作也不显示任何错误。我正在使用 android studio,并且按照官方文档中的建议安装了所有东西。

【问题讨论】:

  • 如果要使用functions.https.onCall需要传参数数据,只是不知道numfirebase.google.com/docs/functions/…的值是多少
  • 我还有一个错误
  • 'PlatformException(functionsError, Cloud function failed with exception., {code: NOT_FOUND, details: null, message: NOT_FOUND})'
  • 如果可能的话帮帮我

标签: flutter google-cloud-firestore google-cloud-functions


【解决方案1】:

您分享的 Flutter 代码实际上还没有调用 Cloud Function。您现在所拥有的只是定义了对该 Cloud Function 的引用。要调用它,您可以执行以下操作:

final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
    functionName: 'getRandomNumbers',
);
dynamic response = await callable.call();

这是来自using Cloud Functions in Flutter 文档中示例的非常直接的副本,因此我建议在实施时保持方便。


我注意到您的 Cloud Function 声明看起来不寻常:

exports.getRandomNumbers=functions.https.onCall((num){

Firebase documentation 中的示例遵循以下模式:

exports.addMessage = functions.https.onCall((data, context) => {

所以参数总是data 和context,你从你的flutter代码中传递的任何参数都将在data参数中。

对于您当前的代码,它没有任何区别,因为您没有使用任何参数,但我确实建议您继续解决这个问题。

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 2019-09-19
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 2022-12-17
    • 2019-06-07
    • 1970-01-01
    • 2021-11-06
    相关资源
    最近更新 更多