How can I do automatic uid for docs in firebase?

Viewed 96

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?

1 Answers

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

Related