title: How to add multiple fields in a document in firebase?
body: <p>I want to save the Device Tokens of some device in a SINGLE DOCUMENT, in DIFFERENT FIELDS.</p>
  <p>Here is my code,</p>
  <pre><code>
  StreamBuilder(
                stream: stream,
                builder: (BuildContext context,
                    AsyncSnapshot&lt;List&lt;DocumentSnapshot&gt;&gt; snapshots) {
                  if (snapshots.connectionState == ConnectionState.active &amp;&amp;
                      snapshots.hasData) {
                    return Expanded(
                      child: ListView.builder(
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        itemCount: snapshots.data.length,
                        itemBuilder: (BuildContext context, int index) {
                          DocumentSnapshot doc = snapshots.data[index];
                          Map location = doc.data;
                          String fieldname = 'f$index';
  
                          var dtoken = location['deviceToken'];
  
                         
                           
  
                            _firestore.collection('deviceTokens').add({
                              fieldname: dtoken,
                            });
                         
                       
                
  
                         
                          return Text(
                            &quot;xyz&quot;,
                          );
                        },
                      ),
                    );
                  } else {
                    return Center(child: CircularProgressIndicator());
                  }
                },
              ),
  
  </code></pre>
  <p>I want to store multiple deviceTokens in a single document.</p>
  <p>To be more clear,</p>
  <p>When this code runs, i want ONLY A SINGLE DOCUMENt created in my database, which is having device tokens in multiple fields like,</p>
  <p>in a single document-</p>
  <p>f1:&quot;1st device token&quot;,</p>
  <p>f2:&quot;2nd device token&quot;,</p>
  <p>fn:&quot;nth device token&quot;</p>
  <p>But this code creates many documents having different device tokens(1 field per document).</p>
  <p>Please help</p>
  <p>UPDATED CODE</p>
  <pre><code>StreamBuilder(
                stream: stream,
                builder: (BuildContext context,
                    AsyncSnapshot&lt;List&lt;DocumentSnapshot&gt;&gt; snapshots) {
                  if (snapshots.connectionState == ConnectionState.active &amp;&amp;
                      snapshots.hasData) {
                    return Expanded(
                      child: ListView.builder(
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        itemCount: snapshots.data.length,
                        itemBuilder: (BuildContext context, int index) {
                          var docid;
  
                          if (index == 0) {
                           _firestore
                                .collection('devicetokens')
                                .add({}).then((value) =&gt; docid = value);
                          }
                          DocumentSnapshot doc = snapshots.data[index];
                          Map location = doc.data;
                          String fieldname = 'f$index';
  
  
                          var dtoken = location['deviceToken'];
  
                          DocumentReference docss =
                              _firestore.collection(&quot;devicetokens&quot;).document(docid);
                          docss.setData({
                            fieldname: dtoken,
                          });
                          
                         
  
                         
                          return Text(
                            &quot;xyz&quot;,
                          );
                        },
                      ),
                    );
                  } else {
                    return Center(child: CircularProgressIndicator());
                  }
                },
              ),
  
  </code></pre>