【发布时间】:2021-12-29 10:02:54
【问题描述】:
我的 Flutter 应用使用 Firbase Cloudfirestore 作为其后端。稍后我想向我的应用程序添加新功能,这将需要 Firestore 文档中的新字段。如何检查文档中是否存在该字段,如果不存在则返回默认值?
Stream<List<Car>> streamCars() {
return _carsCollection.snapshots().map((snapshot) => snapshot.docs.map((document) => Car.fromDocumentSnapshot(document)).toList());
}
static Car fromDocumentSnapshot(DocumentSnapshot snapshot) {
return Car(
id: snapshot.id,
date: snapshot['date'] ?? Timestamp.now(),
seats: snapshot['seats'] ?? 0,
newFeature: snapshot['newFeature'] ?? '', // This field does not exist yet and throws error
);
}
这会引发错误:
Bad state: field does not exist within the DocumentSnapshotPlatform
【问题讨论】:
标签: firebase flutter google-cloud-firestore