title: Create documents, sub collections in Firestore via flutter on screen loads
body: <p>I want to achieve is when flutter screen loads a document should create in firestore in following order.</p>
  
  <pre><code>Document &gt; Sub Collection &gt; Document &gt; Data Fields
  </code></pre>
  
  <p>I manage to create documents and sub collections in above order, but the first <code>Document</code> appear in <code>italic</code>. That's because the <code>child</code> collection, documents creating before <code>parent</code> document created.</p>
  
  <p>But I couldn't able to fix the issue. I've modified the code now it's not even creating the document. Before this It created in <code>italic</code> mode. Now it's not at all.</p>
  
  <p>Here is the code. </p>
  
  <pre><code>getCurrentUser().then((user) {
    DocumentReference todayReference = firestoreInstance.collection('attendance').document(todayDate);
  
    firestoreInstance.collection('profiles').where('user_id', isEqualTo: user).snapshots().listen((onData) {
      onData.documents.forEach((f) {
        CollectionReference todaySubCollection = todayReference.collection(f.documentID);
  
        DocumentReference attendanceReference = todaySubCollection.document(f["name"].toString().toLowerCase());
  
        Map&lt;String,dynamic&gt; mapData = new Map&lt;String,dynamic&gt;();
  
        mapData['attendance_status'] = true;
        mapData['in'] = true;
        mapData['out'] = true;
  
        firestoreInstance.runTransaction((transaction) async {
          await transaction.set(attendanceReference, mapData);
        }); 
  
      });
    });
  });
  </code></pre>
  
  <p>Here <code>getCurrentUser()</code> is returning the logged in <code>user id</code>.</p>
  
  <p>Each <code>profiles</code> assigned to a <code>user</code>.</p>
  
  <p>So, What I'm trying to do is, once user logged in a document should create under <code>attendance</code> collection named <code>today's date</code>. </p>
  
  <p>Then looping through each <code>profiles</code> where <code>user_id</code> is matched with logged in user, the matching results will be store as <code>sub collection</code> under <code>today's date</code> with profiles <code>name</code> field.</p>
  
  <p>Then under the <code>name</code> (document), a transaction needs to run to set details like <code>attendance_status</code>, <code>in</code> &amp; <code>out</code>.</p>
  
  <p>Following images will show how previously documents created.</p>
  
  <p><a href="https://i.stack.imgur.com/xDLf3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xDLf3.png" alt="image 01"></a>
  <a href="https://i.stack.imgur.com/W5ndM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W5ndM.png" alt="image 02"></a></p>
  
  <p>I need to find a way to create documents, collection without in <code>italic</code> mode. Any help would be appreciated.</p>