this is my code
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 //
});
}
how can I get this random generated document's ID? Thank you for your help
To use Flutter with Firebase, you will first need to set dependencies in the pubspec file. You will also have to import firestore , i.e., the database provided by Firebase for data handling. Now, import the Firebase dependencies into your Dart file. import 'package:cloud_firestore/cloud_firestore.
A DocumentReference refers to a document location in a FirebaseFirestore database and can be used to write, read, or listen to the location. The document at the referenced location may or may not exist. A DocumentReference can also be used to create a CollectionReference to a subcollection.
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,
});
Now you can get the document ID:
docRef.documentID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With