Flutter + Firebase Cloud Functions - 如何从 HttpsCallableResult 获取数据

Raú*_*aus 5 firebase flutter google-cloud-functions

我编写了一个简单的云函数,它返回一个保存在我的 Cloud Firestore 数据库中的 id。

云函数如下所示:


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

    return admin.firestore().collection('users').doc('hash').get()
    .then(snapshot => {
        const winkertId = snapshot.wwinkert
        return { id: winkertId };
    })
})

Run Code Online (Sandbox Code Playgroud)

这个函数是从我的 flutter 应用程序中使用以下代码调用的:


Future _getValues() async {
    final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
      functionName: "getWinkert",
    );
    dynamic resp = await callable.call(<String, dynamic>{
      "name": "wwinkert",
    });
    print(resp.toString());
  }

Run Code Online (Sandbox Code Playgroud)

调用该函数后,我得到以下结果:

I/flutter ( 8253): Instance of 'HttpsCallableResult'
Run Code Online (Sandbox Code Playgroud)

问题:如何从 Dart 中的 HttpsCallableResult 实例获取数据?

提前谢谢你的帮助!

你好,劳尔

Raú*_*aus 4

快速浏览一下这个文档:

https://github.com/flutter/plugins/tree/master/packages/cloud_functions

揭示了可以通过将 .data 添加到 HttpsCallableResult 对象来获取数据。

。。。

我很抱歉:D

  • 此链接已损坏 (7认同)
  • 要为其他用户清除问题,您可以像这样获取 id resp.data['id']; (6认同)