Flutter Firestore-查找“文档快照” ID
问题内容:
我有一个带有产品列表的简单应用。产品存储在Firebase Firestore中。我想下载产品列表,并给用户提供更新某些数据的可能性。
因此,我准备了产品清单:
Widget _buildProductsList() {
return new StreamBuilder(
stream: Firestore.instance
.collection('products')
.snapshots,
builder: (context, snapshot) {
if (!snapshot.hasData) return new Text("Loading...");
return new ListView(
children: snapshot.data.documents.map((document) {
Product product = new Product(document.data);
_ProductCell cell = new _ProductCell();
cell.product = product;
return cell;
}).toList(),
);
},
);
}
我将文档快照序列化到我的产品对象:
class Product {
static const NAME_KEY = 'name';
static const DESC_KEY = 'desc';
static const PHOTO_URL_KEY = 'photoUrl';
static const AUTHOR_ID_KEY = 'authorId';
static const CREATED_AT_KEY = 'createdAt';
String name;
String description;
String photoUrl;
String authorId;
int createdAt;
Product(Map<String, dynamic> json) {
name = json[NAME_KEY];
description = json[DESC_KEY];
photoUrl = json[PHOTO_URL_KEY];
authorId = json[AUTHOR_ID_KEY];
createdAt = json[createdAt];
}
}
现在,当用户与ProductCell进行一些交互时,我想更新Firestore中与之链接的Product
Document,但是我没有ID,因此无法创建正确的Firestore引用。
如何实现呢?
问题答案:
由于问题#12471已解决,因此您可以从文档对象获取文档ID。
print(document.documentID)