0
votes

Im trying to create a collection of users in my flutter application.

The application has a stock collection with items which are documents and those documents, like plates or bags, has 2 fields

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).

Ive been told that I have to set the access rules in Firebase to allow to each user to have their own stock collection.

Github https://github.com/juancarlosjr97/flutter_ims

Each user should have their own collection

My database the own it works looks like this: enter image description here

and the other collection with a collection of users looks like this:

enter image description here


This is my code where I get access to the stock collection

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class StockWidget extends StatefulWidget {
  @override
  StockWidgetApp createState() => new StockWidgetApp();
}

class StockWidgetApp extends State<StockWidget> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      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<DocumentSnapshot> snapshot) {
    return ListView(
      padding: const EdgeInsets.only(top: 20.0),
      children: snapshot.map((data) => _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: () =>
              record.reference.updateData({'instock': record.instock + 1}),
          onLongPress: () =>
              record.reference.updateData({'instock': record.instock - 1}),
        ),
      ),
    );
  }
}

class Record {
  final String item;
  final int instock;
  final DocumentReference reference;

  @override
  String toString() => "Record<$item:$instock>";

  Record.fromMap(Map<String, dynamic> 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);
}
1
Do you want to create stock collection for each User document with coding? - Armaan Sandhu
Please provide the minimal code that reproduces the problem inside your question itself. Also make sure to have a (single) question in your post, as right now it is unclear to me what you're asking. - Frank van Puffelen
Sorry, Yes, Im trying to create a stock collection for each user. The idea is that each user has their own stock and not a common stock list like is know - juancarlosjr97

1 Answers

0
votes

Use this collection for stock :

final userId = [get the user id from auth]
final col = Firestore.instance.collection("users").document(userId).collection("stock");

//add stock
col.add(yourstock);