You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用云函数获取数据时出现Closure: () => Map<String, dynamic>报错

问题原因

你看到的Closure: () => Map<String, dynamic> from Function 'data':不是运行时错误,是你访问DocumentSnapshot的data时没有调用方法导致的:
在Flutter的cloud_firestore库中,DocumentSnapshot的data是方法而非属性,直接访问会返回方法本身的闭包描述,只有调用data()才能拿到实际的文档数据。

修正代码

你只需要修改get()回调里的打印语句即可:

await _classLoginRef
    .get().then((onValue) {
  isClassPresent = onValue.exists;
  // 给data加()调用方法
  print("Inside Then :" + onValue.data().toString());
});

额外优化建议

你同时用了await和.then回调写法,更推荐直接用await的同步写法,逻辑更清晰不容易出错:

// 替换原来的await + then的代码段
final classLoginDoc = await _classLoginRef.get();
isClassPresent = classLoginDoc.exists;
print("文档内容:${classLoginDoc.data()}");

注:你备注提到当前使用Typescript开发,但贴出的是Flutter对应的Dart代码,注意不要把两种语法搞混,避免出现其他语法问题。

内容的提问来源于stack exchange,提问作者Chhay Keokanitha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 08:27:02