简体   繁体   中英

how to get seller id and product id in flutter firebase in one field for example, product field

FirebaseFirestore.instance
    .collection('products')
    .add({
  // how to add product id and seller id
  'image': imageUrls,
  'name': name,
  'category': category,
  'description': description,
  'price': 'RM' + ' ' + price,
  'weight': weight + 'gram',
  'stock': stock,
}).then((value) {
 

i don't know how to declare the difference seller when seller add their product to the seller homepage at my stream builder. also, the declaration of seller id and product id at the below mapping add data to firebase

You can genrate a new random productID and for sellerID u can use userID of seller who adds the product.

FirebaseAuth firebaseAuth = FirebaseAuth.instance;
FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;

CollectionReference collectionReference = firebaseFirestore.collection('products');
String docID = collectionReference.doc().id;

collectionReference.doc(docID).set({
'productID': docID,
'sellerID': firebaseAuth.currentUser!.uid, //(it is the userID of the seller who is adding this product)
'image': imageUrls,
'name': name,
'category': category,
'description': description,
'price': 'RM' + ' ' + price,
'weight': weight + 'gram',
'stock': stock,
}).then((value) { 
 // write your other logic here
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM