我正在尝试从 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();
}
附上关于错误的截图:
答案 0 :(得分:1)
根据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();
}
答案 1 :(得分:0)
您可能正在寻找 document.data[] 而不是 document.data()[]
document.data 是一个返回 Map
所以这段代码应该可以工作:
List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((document) {
return Brew(
name: document.data['name'] ?? '',
strength: document.data['strength'] ?? 0,
sugars: document.data['sugars'] ?? '0',
);
}).toList();
}