【问题标题】:What is a method of using Firebase Cloud Functions in Flutter.在 Flutter 中使用 Firebase Cloud Functions 的方法是什么。
【发布时间】:2018-10-11 02:02:33
【问题描述】:

我一直在搜索如何使用颤振应用程序实现 firebase 功能。似乎没有可用的 SDK(还)。我还尝试将 gradle 依赖项 implementation 'com.google.firebase:firebase-functions:15.0.0' 添加到我的 app/build.gradle 中,但这会导致构建错误。

有人做过有效的实现吗?我找不到任何有关如何处理凭据和数据传输以构建我自己的 firebase 函数调用的文档。

我已经大致勾勒出我认为这是如何工作的,但可能有点离谱。

Future<dynamic> updateProfile(String uid, AccountMasterViewModel avm) async {

    Uri uri = Uri.parse(finalizeProfileFunctionURL);
    var httpClient = new HttpClient();
    String _result = '';

    try {
      return await httpClient
        .postUrl(uri)
        .then((HttpClientRequest request) {
          return request.close();
          // authentication??
          // Fields and data??
        })
        .then((HttpClientResponse response) async {
            print(response.transform(new Utf8Codec().decoder).join());
            if (response.statusCode == HttpStatus.OK) {
              String json = await response.transform(new Utf8Codec().decoder).join();
              _result = jsonDecode(json);
              // Do some work
              return json;
            } 
            else {
              return ':\nHttp status ${response.statusCode}';
            }
          });
        }
        catch (exception) {
          return 'Failed ' + exception.toString();
        }
      }

我希望能够发送一个对象,例如

{
    accountID: src.accountID, 
    accountName: src.name,
    accountImg: src.image
}

然后处理响应。但正如我所说,我找不到任何关于如何做到这一点的工作示例或教程。做这个客户端大小并直接与数据库对话是相当简单的,但是,有一些验证和数据组件需要对客户端隐藏,所以云功能是我想要这样做的方式。

【问题讨论】:

  • 实际上从 Flutter 应用程序的角度来看,HTTPS 云函数就像一个 REST API(除非游戏中有一些 Firebase 身份验证......)。例如。您将对象发布到端点并取回对象(或错误)。因此,由于它是后端的云功能,因此您不应该遇到任何困难。您不应在 Flutter 代码中声明任何有关 CF 的内容,例如 implementation 'com.google.firebase:firebase-functions:15.0.0
  • 关于如何对 HTTP 请求使用身份验证,请参阅stackoverflow.com/questions/48477625/…

标签: firebase google-cloud-functions flutter


【解决方案1】:

在 Flutter 中调用 Firebase 的 Cloud Functions 的更新答案是

var callable =  CloudFunctions.instance.getHttpsCallable(functionName: 'functionName'); // replace 'functionName' with the name of your function
dynamic response = callable.call(<String, dynamic>{
    'param1': param1 //replace param1 with the name of the parameter in the Cloud Function and the value you want to insert
  }).catchError((onError) {
        //Handle your error here if the function failed

  });

【讨论】:

    【解决方案2】:

    是的,这里有一个 cloud_function 包:https://pub.dartlang.org/packages/cloud_function

    以便调用你可以调用的函数

    CloudFunctions.instance.call(
                        functionName: 'yourCloudFunction',
                        parameters: <String, dynamic>{
                          'param1': 'this is just a test',
                          'param2': 'hi there',
                        },
                      );
    

    【讨论】:

    • 最终 HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(functionName: 'cloudFunction');动态响应 = 等待 callable.call(getAllTokens);我传递了一个数组'getAllTokens'。你能建议我一个函数吗,我在 index.js 的云函数中获取所有这些令牌
    【解决方案3】:

    云功能可以由实时数据库、Firestore 或 Datastore 中的数据更改触发器以及身份验证触发器触发。

    你可以坚持

    {
        accountID: src.accountID, 
        accountName: src.name,
        accountImg: src.image
    }
    

    到数据库并注册一个触发器,该触发器在插入、更新或删除特定路径的数据时运行云函数。

    https://firebase.google.com/docs/functions/firestore-events

    【讨论】:

      【解决方案4】:

      这是一个很好的关于 Flutter 中云功能的教程,对我有帮助:

      https://rominirani.com/tutorial-flutter-app-powered-by-google-cloud-functions-3eab0df5f957

      【讨论】:

        猜你喜欢
        • 2020-12-12
        • 2017-08-08
        • 2019-02-07
        • 2017-08-22
        • 2018-11-16
        • 1970-01-01
        • 1970-01-01
        • 2020-03-15
        • 2020-02-21
        相关资源
        最近更新 更多