How to post line_items in json file through woocommerce rest api?

Question
class _CheckoutState extends State<Checkout> {
  @override
  Widget build(BuildContext context) {
    final cart = Provider.of<Cart>(context); 
    Future<Orders>createOrder(fname,lname,email,address,city,phone)async{
  try {
List<CartPdt>linecartitems=List<CartPdt>();
linecartitems.map((i)=>{
  'product_id':int.parse(i.id),
  'quantity':i.quantity
}).toList();
String lc=jsonEncode(linecartitems);
print(lc);

  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"},
  );
  var jsondata=null;
  jsondata=createOrderUrl.body;
  print(createOrderUrl.statusCode);
  print(createOrderUrl.body);
  print(email);
  return jsondata;
}catch(error){
  throw error;
}}


CartPdt class

class CartPdt extends StatelessWidget {

   String id;
  String productId;
   double price;
  int quantity;
  String name;
  CartPdt(this.id,this.productId,this.price, this.quantity, this.name);

  CartPdt.fromJson(Map<String, dynamic> json) {
    productId = json['product_id'];
    name = json['name'];
    quantity = json['quantity'];
  price=json['price'];}

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();

    data['product_id'] = this.productId;
    data['name'] = this.name;
    data['quantity'] = this.quantity;
    data['price']=this.price;
    return data;
  }
0
Maida Farooqi 2 years 2020-10-22T06:10:35-05:00 0 Answers 8 views 0

Leave an answer

Browse
Browse