title: Flutter Firebase global Auto Incremental value and retrieving in document field dart
body: <p>I'm quite new to Firebase Flutter. I'm developing a mobile application to share books among others.</p>
  <p>In firebase firestore,</p>
  <ul>
  <li>I have 'users' collections which contain all the user data with unique id</li>
  <li>I have 'books' collection which contain all the book data with unique id created automatically</li>
  <li>Also I have 'global' collection with single document with one integer field called 'bookcount'.</li>
  <li>Users can can have many books.</li>
  </ul>
  <p>Now I want to create a another unique id field for book. idea is to have simple integer id.
  One way of doing this is get list of books and find the length (count) and add 1 when creating a new record. I have ruled out this method as if many users using simultaneously, I think this can lead to duplicate ids.</p>
  <p>So I have created a another collection global with single document and field name bookcount. Which hold number of books (rough count) on books collection. So idea is each time when adding a book to a collection increase bookcount and use this value as simple unique id for a book. This bookcount may not represent actual books as user can discard the book entry before saving it, which is okay as I only need a simple unique id.</p>
  <pre><code>class DatabaseService {
        ...
        ...
    
    //final CollectionReference bookCollection = Firestore.instance.collection('users');
    //final CollectionReference bookCollection = Firestore.instance.collection('books');
    final CollectionReference globalData = Firestore.instance.collection('global');
        ...
        ... 
  Future&lt;String&gt; bookId() async 
      {
          String uniquebookid =  await globalData.document('SomeHardcodedID').updateData(
          {
          'bookcount': FieldValue.increment(1)
          }).then((voidvalue) async 
          {
              String cid = await globalData.getDocuments().then((bookvalue) =&gt; bookvalue.documents.single.data['bookcount'].toString());
              return cid;
          });
      return uniquebookid;
      }//future bookId
        ...
        ...
  }//class
  </code></pre>
  <p>Now this works. well somewhat, Can we do this better? In here there are two parts, first increment the value bookcount, and then retrieve it.</p>
  <ol>
  <li>Can we do this in one go?</li>
  </ol>
  <p>If I try to call this method consecutively really fast when returning a value it might skip few numbers. I have call this from a button and try to press as fast I could. I think counter increase but it return
  same number few times. and then skip some when press again. for example  1,2,3,4,8,8,8,8,9,10,... So at counter 4 I try to press the button multiple times. I wonder how this will behave when multiple users adding multiple books at the same time.</p>
  <ol start="2">
  <li>How Can I fix this?</li>
  </ol>
  <p>Please Help, Thanks.</p>