【问题标题】:How to post line_items in json file through woocommerce rest api?如何通过 woocommerce rest api 在 json 文件中发布 line_items?
【发布时间】:2021-02-05 06:02:37
【问题描述】:

我已经在我的颤振应用程序中成功使用了 woocommerce 的 rest api,但我被困在一个点,即通过 woocommerce rest API 在 json 文件中添加 line_items。

List<CartPdt>linecartitems=[];
linecartitems.map((i)=>{
  'product_id':int.parse(i.id),
  'quantity':i.quantity
}).toList();

var lc=json.encode(linecartitems);

var createOrderUrl = await http.post(
    'url',
    body: json.encode({
      'billing': {
        'first_name': fname,
        'last_name': lname,
        'email': email,
        'address': address,
        'city': city,
        'phone': phone
      },
      'line_items':lc,
    }),
    headers: {"Content-Type": "application/json"},
  );

【问题讨论】:

  • 请说明您遇到的错误或问题。
  • "line_items":"line_items[0] 不是对象类型。这是我遇到的错误。
  • 你可以看看这个答案:stackoverflow.com/a/55025048/10659482

标签: flutter woocommerce-rest-api


【解决方案1】:

您需要将列表模型转换为 JSON。我假设您的模型中没有 toJson 方法。添加这个:

Map<String, dynamic> toJson(){
  return {
    "product_id": this. product_id,
    "quantity": this. quantity
  };
}

【讨论】:

  • 不,我已经在 CartPdt 类中添加了 toJson 和 fromJson 方法..
猜你喜欢
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 2021-07-03
  • 2016-03-23
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 2017-06-06
相关资源
最近更新 更多