title: Collection per Users FireStore Flutter App
body: <p>Im trying to create a collection of users in my flutter application. </p>
  
  <p>The application has a stock collection with items which are documents and those documents, like plates or bags, has 2 fields</p>
  
  <p>I create a root users collection and that collection documents which identified the used with their user ID, one document for each user ID (the one you get from FirebaseAuth).</p>
  
  <p>Ive been told that I have to set the access rules in Firebase to allow to each user to have their own stock collection. </p>
  
  <p>Github <a href="https://github.com/juancarlosjr97/flutter_ims" rel="nofollow noreferrer">https://github.com/juancarlosjr97/flutter_ims</a></p>
  
  <p>Each user should have their own collection</p>
  
  <p>My database the own it works looks like this: <img src="https://i.stack.imgur.com/8KmXq.png" alt="enter image description here"></p>
  
  <p>and the other collection with a collection of users looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/lfFaj.png" alt="enter image description here"></p>
  
  <hr>
  
  <p>This is my code where I get access to the stock collection </p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:cloud_firestore/cloud_firestore.dart';
  
  class StockWidget extends StatefulWidget {
    @override
    StockWidgetApp createState() =&gt; new StockWidgetApp();
  }
  
  class StockWidgetApp extends State&lt;StockWidget&gt; {
    @override
    Widget build(BuildContext context) {
      return StreamBuilder&lt;QuerySnapshot&gt;(
        stream: Firestore.instance.collection('stock').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return LinearProgressIndicator();
          return _buildList(context, snapshot.data.documents);
        },
      );
    }
  
    Widget _buildList(BuildContext context, List&lt;DocumentSnapshot&gt; snapshot) {
      return ListView(
        padding: const EdgeInsets.only(top: 20.0),
        children: snapshot.map((data) =&gt; _buildListItem(context, data)).toList(),
      );
    }
  
    Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
      final record = Record.fromSnapshot(data);
  
      return Padding(
        key: ValueKey(record.item),
        padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(color: Colors.grey),
            borderRadius: BorderRadius.circular(5.0),
          ),
          child: ListTile(
            title: Text(record.item),
            trailing: Text(record.instock.toString()),
            onTap: () =&gt;
                record.reference.updateData({'instock': record.instock + 1}),
            onLongPress: () =&gt;
                record.reference.updateData({'instock': record.instock - 1}),
          ),
        ),
      );
    }
  }
  
  class Record {
    final String item;
    final int instock;
    final DocumentReference reference;
  
    @override
    String toString() =&gt; "Record&lt;$item:$instock&gt;";
  
    Record.fromMap(Map&lt;String, dynamic&gt; map, {this.reference})
        : assert(map['item'] != null),
          assert(map['instock'] != null),
          item = map['item'],
          instock = map['instock'];
  
    Record.fromSnapshot(DocumentSnapshot snapshot)
        : this.fromMap(snapshot.data, reference: snapshot.reference);
  }
  </code></pre>