I would like to set a custom document ID to new entries in my cloud Firestore but i cant seem to figure out how to do it, here is my code, it still generates a random ID
FirebaseFirestore.instance.collection("payment_links").add(
{'id': user_id.toString(),
'link':
paymentlink.toString(), 'link_id': linker.toString(),
'amount':
c1.text.toString(),
'title':
c2.text.toString(),
'trxnid':
secureRandom.nextString(
length: 15, charset: '0123456789').toString(),
'payments' : 0
}).then((value){
print("================================sucess=============================================================");
print(value.documentID);
});
Solution 1: Doug Stevenson
If you don't want an automatic random ID, then don't use add()
. You should build a DocumentReference to the document you want to create, and use set() instead.
DocumentReference ref = FirebaseFirestore.instance
.collection("payment_links")
.doc("your-document-id");
ref.set({ ... });
I suggest reading the documentation carefully where it says:
If you'd like to specify your own ID, call the set method on a DocumentReference instead