How to get current user auto generated doc id from flutter firestore?

Viewed 42

I'm trying to get the current auto-generated user doc id to show and update data after login but getting a different id. Kindly help Thank you. enter image description here

1 Answers

You should call the document method on whichever collection you're trying to acquire a document from, and the returned result will include a getter for documentID.

final userDocument = usersCollection.document(); final documentID = userDocument.documentID;

When you initially want to create a document, you typically do this. I would include the documentID in the user document itself so that you can get it after you've produced it:

userDocument.setData({ documentID: documentID, /* ... */ });

document() Without specifying a route, this method will generate a random ID for you. According to the documentation, an auto-generated ID is used if no [path] is specified. A client-generated date is prefixed to the unique key created to ensure that the resulting list is chronologically ordered.

Related