firebase - 未为类型“对象”定义运算符“[]”
问题描述
我正在尝试从 Firestore 获取 brews 集合中的文档。
我的代码有什么问题?
帮助我熟悉 Flutter 和 Firebase 的人。
////brew list from snapshot
List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((document) {
return Brew(
name: document.data()['name'] ?? '',
strenght: document.data()['strength'] ?? 0,
sugars: document.data()['sugars'] ?? '0',
);
}).toList();
}
附上有关错误的屏幕截图:
解决方案
根据cloud_firestore插件github:
/// A [DocumentSnapshot] contains data read from a document in your [FirebaseFirestore]
/// database.
///
/// The data can be extracted with the data property or by using subscript
/// syntax to access a specific field.
因此,也许对您的代码的这种修改会起作用:
List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((document) {
return Brew(
name: document['name'] ?? '',
strength: document['strength'] ?? 0,
sugars: document['sugars'] ?? '0',
);
}).toList();
}
推荐阅读
- mongodb - 在 Mac 上运行 MongoDB 的问题“无法锁定锁定文件”
- regex - 正则表达式:不需要的字符
- java - 缺少 javax.sip.STACK_NAME 属性和 gov.nist.javax.sip.SipStackImpl 无法在 Java 中实例化
- python - 如何从复制 .zip 文件中取得进展
- java - 一旦我为 kafka 服务器运行命令,Zookeeper 就会自动关闭
- jmeter - JMeter - 采样器中的折叠选项
- assembly - OpenCL 在线编译:从 cl::program 或 cl::kernel 获取程序集
- amazon-web-services - 使用 AWS API Gateway 的服务器端获取请求
- javascript - 在标题上选择过滤器并修复 - 错误的选项值
- python-3.x - 如何在 python 函数中并行运行多个 jira search_issues 查询?