Creating Sub-Collection in Document from Flutter app
final databaseReference = Firestore.instance;
databaseReference.collection('main collection name').document( unique id).collection('string name').document().setData(); // your answer missing **.document()** before setData
this is the right Syntex
Figured out the problem. I was using the wrong syntax. The correct syntax is
Firestore.instance.collection('path').document("documentPath").collection('subCollectionPath').setData({});
The key difference here is that the forward slashes have been removed from the paths name.
Here's the solution, if you want to create a subcollection within a Firestore Transaction:
Firestore.instance.runTransaction((Transaction tx) {
tx.set(Firestore.instance.collection('path').document('documentPath')
.collection('subCollectionPath').document(), {'TestData', 'Data'});
})
NOTE: You cannot create an empty subcollection, you must create the subcollection with at least one document. If you try to create the subcollection with the data field empty, Firestore will create an empty document.