Firestore/Flutter-如何获取文件ID? [英] Firestore / Flutter - How can I get document Id?
本文介绍了Firestore/Flutter-如何获取文件ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
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?谢谢您的帮助
how can I get this random generated document's ID? Thank you for your help
推荐答案
在 collection
之后,您可以添加 document
并接收 DocumentReference
.
After collection
you can add a document
and receive the 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:
Now you can get the document ID:
docRef.documentID
这篇关于Firestore/Flutter-如何获取文件ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文