在 Flutter 中使用 Firebase Cloud Functions 的方法是什么?



我一直在搜索如何使用颤振应用程序实现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
}

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

是的,这里有一个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',
                    },
                  );

在 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
  });

这是关于颤振云函数的好教程,它帮助了我:

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

云函数可以通过实时数据库、Firestore 或数据存储中的数据更改触发器以及身份验证触发器触发。

你可以坚持下去

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

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

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

相关内容

最新更新