If you want to get the document id after adding document in Cloud Firestore in Dart. You can try the following:
1 2 3 | DocumentReference docRef = await Firestore.instance.collection('gameLevels').add(map); print(docRef.documentID); |
Since add() method creates new document and autogenerates id, you don’t have to explicitly call document() method
Or if you want to use “then” callback try the following:
1 2 3 4 5 6 7 8 | final collRef = Firestore.instance.collection('gameLevels'); DocumentReferance docReference = collRef.document(); docReferance.setData(map).then((doc) { print('hop ${docReferance.documentID}'); }).catchError((error) { print(error); }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.