I need to get and save the document Id from a newly created document. How do I return this id from the Future or how do I add the docRef.id to the client provider?
Future<void> saveNewClient(client) async {
try {
DocumentReference docRef = await _db.collection('client').add(client);
client.updateClientId(docRef.id); <<<< CLIENT PROVIDER
} catch (e) {
print(e);
}
return;
}
This code does not throw an error but it never reaches the client.updateClientId(docRef.id) line. The save works so I should be able to get the docRef.id.
Why is this line being skipped?
2
Answers
After creating a document, you can retrieve its document ID from the DocumentReference object returned by the add() method.
There are a few ways to achieve this depending your preference or use case