这是我的代码
Future _save() async {
final StorageReference storageRef = FirebaseStorage.instance.ref().child('product/'+nameController.text+'.jpg');
final StorageUploadTask task = storageRef.putFile(_image);
StorageTaskSnapshot taskSnapshot = await task.onComplete;
String downloadUrl = await taskSnapshot.ref.getDownloadURL();
StorageMetadata created = await taskSnapshot.ref.getMetadata();
Firestore.instance.collection('product').document()
.setData({
'name': nameController.text,
'price': int.tryParse(priceController.text),
'description': descriptionController.text,
'creator': widget.user.uid,
'created': DateTime.fromMillisecondsSinceEpoch(created.creationTimeMillis, isUtc: true).toString(),
'modified': DateTime.fromMillisecondsSinceEpoch(created.updatedTimeMillis, isUtc: true).toString(),
'url': downloadUrl,
'id': //I want to set document Id here //
});
}
我怎样才能得到这个随机生成的文档的 ID?
感谢您的帮助
最佳答案
后 collection
您可以添加 document
并收到 DocumentReference
.
final docRef = await Firestore.instance.collection('product').add({
'name': nameController.text,
'price': int.tryParse(priceController.text),
'description': descriptionController.text,
'creator': widget.user.uid,
'created': DateTime.fromMillisecondsSinceEpoch(created.creationTimeMillis, isUtc: true).toString(),
'modified': DateTime.fromMillisecondsSinceEpoch(created.updatedTimeMillis, isUtc: true).toString(),
'url': downloadUrl,
});
现在您可以获取文档 ID:
docRef.documentID
关于firebase - Firestore/Flutter - 如何获取文档 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53605611/