【问题标题】:How to structure data for flutter Cart flutter?如何为颤振购物车颤振构造数据?
【发布时间】:2021-01-26 13:33:15
【问题描述】:

我正在为 firebase 的颤振电子商务购物车数据结构而苦苦挣扎,构建数据的正确方法是什么?

现在我在 firebase 上像这样构建它:

cart_items = [
        {
            "id": 144,
            "created_at": "2019-04-04 14:42:04",
            "updated_at": "2019-04-04 14:42:04",
            "cart_id": "3",
            "client_id": "83",
            "product_id": "6",
            "quantity": "1",
            "price": "1500",
            "name": "Cucumber (2Pcs)",
            "image": "products/es4eGjkgQ6MvzTaMyX4iXWjcSX03mVk3QB9oODWk.jpeg",
           },
        {
            "id": 145,
            "created_at": "2019-04-04 14:42:09",
            "updated_at": "2019-04-04 14:42:09",
            "cart_id": "3",
            "client_id": "83",
            "product_id": "5",
            "quantity": "1",
            "price": "2000",
            "name": "Cauliflower",
            "image": "products/lVZ31zORzltyVIDXhHoCWUgjTlal7cWd7pI8DL2V.jpeg",
            }
    ]

但问题是我无法更新产品的数量字段。

【问题讨论】:

  • "我无法更新产品的数量字段。"为什么不?是什么阻止你这样做?一般来说,如果您在问题中包含minimal code that reproduces where you got stuck,您将在 Stack Overflow 上获得更好的帮助。遵循该链接中的指导会大大增加有人提供帮助的机会。
  • 好吧,我无法更新 quantity 字段,因为它存在于地图中,而地图中存在于数组中。

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

quantity 字段必须是整数或数字而不是字符串,然后您可以使用 FieldValue.increment(itemCount) 来更新数量值,我也看到了 cart_idclient_idproduct_idprice字段也是字符串,改成整数或数字

【讨论】:

  • 如何更新它?我确实通过将数量转换为整数来尝试 FieldValue.increment 方法,但我面临的问题是更新数量的字段。
  • const userRef = db.collection('documents').doc('document_id');常量增量 = firebase.firestore.FieldValue.increment(1); userRef.update({ FieldToIncrease: increment });
猜你喜欢
  • 2021-09-22
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 2018-12-09
  • 2020-12-18
  • 2021-05-31
  • 1970-01-01
相关资源
最近更新 更多