Flutter:get document id form firstore when setData is called
1,220
Solution 1
you have to either set it yourself:
DocumentReference _skillsRef = Firestore.instance.collection('skills').document(skillsID);
or auto-generate it:
DocumentReference _skillsRef = Firestore.instance.collection('skills').document();
and that ref is what you set it on:
_skillsRef.setData({
'bite_id': widget.biteId,
'start_date': widget.startDate,
'frequency': 3
})
Solution 2
Don't use an empty document()
or setData
, instead seData
use add
like this :
Future<void> createCommitment() async {
DocumentReference docRef = await widget._firestore
.collection('skills')
.document(widget.skillID)
.collection("commitment")
.add({
'bite_id': widget.biteId,
'start_date': widget.startDate,
'frequnecy': 3,
});
//Here you have the autogenerated ID :
print("ID : ${docRef.documentID}");
}
Author by
M.Ali
Updated on December 08, 2022Comments
-
M.Ali over 1 year
How to get the newly created document id when we call setData
Future<void> createCommitment() async { await widget._firestore .collection('skills') .document(widget.skillID) .collection("commitment") .document() .setData({ 'bite_id': widget.biteId, 'start_date': widget.startDate, 'frequnecy': 3 }); }
the current implementation returns void