Home > Net >  How can i add the sub collection in a data model? flutter
How can i add the sub collection in a data model? flutter

Time:10-27

how can i get the list products in my sub collection and add it to the data model i created. I am trying to get all the products added to cart from a vendor.. here is the structure of my database enter image description here

and here is the data code i am trying to do

class cartModel {
      vendorCartModel? vendorCart;
      List<productCartModel>? productsInCart;
    
      cartModel();
    
      cartModel.fromSnapshot(DocumentSnapshot snap){
        vendorCart = vendorCartModel.fromSnapShot(snap);
        productsInCart = ?????? (here is the part where i don't know what to do')
      }
      }
================

class vendorCartModel {
  bool selected = false;
  String? storeName;
  String? uid;
  String? userId;
  String? vendorToken;
  GeoPoint? storeLocation;

  vendorCartModel();
  vendorCartModel.fromSnapShot(DocumentSnapshot snap){
    storeName = snap["storeName"];
    uid = snap["uid"];
    userId = snap["userId"];
    vendorToken = snap["vendorToken"];
    storeLocation = snap["storeLocation"];
  }
}
==========================

class productCartModel {
  bool selected = false;
  String? name;
  List<dynamic>? imgUrl;
  String? uid;
  String? vendorUid;
  int? quantity;
  double? sellingPrice;
  double? sellerPrice;
  double? oldPrice;
  double? weight;
  bool? shipping;
  int? minOrder;
  
  productCartModel();

  productCartModel.fromSnapshot(DocumentSnapshot snap){
    uid = snap["uid"];
    vendorUid = snap["vendorUid"];
    imgUrl = snap["imgUrl"];
    name = snap["name"];
    weight = snap["weight"];
    sellingPrice = double.parse(snap["sellingPrice"].toString());
    sellerPrice = double.parse(snap["sellerPrice"].toString());
    oldPrice = double.parse(snap["oldPrice"].toString());
    quantity = snap["quantity"];
    minOrder = snap["minOrder"];
    shipping = snap["shipping"];
  }
}

================ here is my structure of database

  • cart (collection)
    • fields (this will be used as vendor)
      • products ( sub collection )

============== this is my query

await _fStoreIns
            .collection('cart')
            .doc(vendorId).get().then((value){
cartModel = cartModel.fromSnapshot(value);
})

Thanks in advance.

CodePudding user response:

For Updating data

final pushData = FirebaseFirestore.instance.collection('cart');
pushData
        .doc(cardId)
        .collection("products")
        .doc(productId)
        .set({
         changes:"Changes"
         })

For adding data

final pushData = FirebaseFirestore.instance.collection('cart');
pushData
        .doc(cardId)
        .collection("products")
        .add({
          data: "DATA"
        });

For getting data

await pushData
        .doc(productid)
        .collection("products")
        .orderBy("timeStamp", descending: true) // if you want to order by time or whatever
        .limit(50) //for adding limit add limit
        .get();

CodePudding user response:

if I understood correctly you have the following structure in fbCloudStore

-vendors (collection)
 -vendor (doc)
  -products (colecction)
   -product (doc)

To get the specific subcollection you can do:

final vendorId = 'Replace with vendor Id';
    final _querySnapshot = await _fStoreIns
            .collection('vendors')
            .doc(vendorId)
            .collection('products')
            .get();
    
    
    for(final doc in _querySnapshot.docs){
      //do stuff here
      ProductModel.fromJson(doc.data());
    }

CodePudding user response:

my structure is like this

- cart (collection)
  - fields (this will be used as vendor)
    - products ( sub collection )

what i wanted to get is something like this

await _fStoreIns
            .collection('cart')
            .doc(vendorId).get().then((value){
cartModel = cartModel.fromSnapshot(value);
})

where in inside my cartModel.fromSnapshot is

cartModel.fromSnapshot(DocumentSnapshot snap){
    vendorCart = vendorCartModel.fromSnapShot(snap);
    productCart.add(ChkOutItemModel.fromSnapshot(snap));
  }

CodePudding user response:

First, we get the vendor's data and then the productsData and pass both to cartModel.fromSnapShot method.

Use this query:

DocumentSnapshot<Map<String, dynamic>> vendorData =
    await _fStoreIns.collection('cart').doc(vendorId).get();
QuerySnapshot<Map<String, dynamic>> productsData = await _fStoreIns
    .collection('cart')
    .doc(vendorId)
    .collection('products')
    .get();
cartModel = cartModel.fromSnapshot(vendorData, productsData);

Define your cartModel.fromSnapshot as below:

cartModel.fromSnapshot(DocumentSnapshot vendorData, QuerySnapshot productsSnap) {
  vendorCart = vendorCartModel.fromSnapShot(vendorData);
  productsInCart = productsSnap.docs
      .map((e) => productCartModel.fromSnapShot(productsSnap));
}
  • Related