我有一个带有产品列表的简单应用程序。产品存储在 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 中链接的产品文档,但我没有 ID,因此无法创建正确的 Firestore 引用。
如何实现这一目标?
由于问题 #12471 已解决,您可以从文档对象获取文档 ID。
print(document.documentID)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)