Diego Gamboa
Diego Gamboa

Reputation: 43

How can I do automatic uid for docs in firebase?

Im trying to do a group system for a flutter app and I need to store it in firebase, and every group has his own ID, it's called "groupID". How to do programmatically that every new group create has his "groupID" create automatically. Is organized like this:

enter image description here

In other words, how can I do in the code to fill the Groups column?

Upvotes: 1

Views: 214

Answers (1)

Muthu Thavamani
Muthu Thavamani

Reputation: 1405

In Flutter,

  1. Let firestore creates document reference with it's automatic document ID
DocumentReference docRef = firestore.collection('Groups').document();
print(docRef.documentID);
docRef.setData({JSON Data});
  1. Providing your own document ID for groups
String groupID = 'unique_id'
DocumentReference docRef = firestore.collection('Groups').document(groupID);
print(docRef.documentID);
docRef.setData({JSON Data});

For constraints on document ID - https://firebase.google.com/docs/firestore/quotas#collections_documents_and_fields

Upvotes: 2

Related Questions