1

i have this json i want to post to an api but i am not sure how it is done ..this is how the request looks like :

  {
    "products": [
        {"id":1, "qty": 1},{"id":2, "qty": 1}
    ],
    "vendor_id": 1,
    "notes": " ",
    "address": ""
}

and this is the request class that i used to map the request :

    class Order{

  int vendor_id;
  String address ,notes ;
  List<OrderProduct> products;

  Order({this.products , this.address , this.notes , this.vendor_id});

  Map<String, dynamic> toMap() {
    return {
      'vendor_id': vendor_id,
      'address': address,
      'notes': notes,
      'products': products,
    };
  }


}

class OrderProduct{
  int id , qty ;
  OrderProduct({this.id , this.qty});
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'qty': qty,
    };
  }
}

what am i missing out ?

2 Answers 2

1

Just Do it like json.encode(myArr), for example

 Map<String, String> params = {
      "mobile": userNameController.text,
      "password": passwordController.text,
      "deviceid": '${fcmToken}',
    };
 Future<String> postRequest(var url, {var postParams}) async {
    return http
        .post(url, body: json.encode(postParams))// do json encoding here 
        .then((http.Response response) {
      final int statusCode = response.statusCode;
      print("postParams " + json.encode(postParams));
      print("statusCode " + '${statusCode} ${response.body}');
      if (statusCode < 200 || statusCode > 400 || json == null) {
        throw new Exception("Error while fetching data");
      }
      print(response.request);
      return response.body;
    });
  }
Sign up to request clarification or add additional context in comments.

Comments

0

Json that I have posted in body..

{
       "products": [
          {"id":1, "qty": 1},{"id":2, "qty": 1}
       ],
       "vendor_id": 1,
       "notes": " ",
       "address": ""
    }

Your Post Api call be like:

Future<Order> postOrder(int vendor_id, String notes, String address, int 
 id1,int q1, int id2, int q2) async {

 Paste your api url here
 String url = '';
 final response = await http.post(apiUrl, headers: {
   // Enter your headers parameter if needed
   //  E.g: 
  'Authorization' : 'xyz',
  'Content-Type' : 'application/json'
 },
body: jsonEncode(<String, String>{
  'vendor_id' : vendor_id,
  'notes' : notes,
  'address' : address,
  'products' : [
   {'id': id1,'qty' : q1},
   {'id' : id2,'qty' : q2}
  ]
}));

  if (response.statusCode == 200) {
    var data = jsonDecode(response.body.toString());
    print(data);
     return Order.fromJson(jsonDecode(response.body));
   } else {
  throw Exception('Failed to post user.');
  }
 }

Your model be like:

class Order {
    List<Products>? products;
     int? vendorId;
     String? notes;
     String? address;

Order({this.products, this.vendorId, this.notes, this.address});

Order.fromJson(Map<String, dynamic> json) {
   if (json['products'] != null) {
     products = <Products>[];
     json['products'].forEach((v) {
       products!.add(new Products.fromJson(v));
     });
    }
    vendorId = json['vendor_id'];
    notes = json['notes'];
    address = json['address'];
  }

 Map<String, dynamic> toJson() {
   final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.products != null) {
      data['products'] = this.products!.map((v) => v.toJson()).toList();
    }
    data['vendor_id'] = this.vendorId;
    data['notes'] = this.notes;
    data['address'] = this.address;
    return data;
  }
 }

  class Products {
    int? id;
    int? qty;

    Products({this.id, this.qty});

    Products.fromJson(Map<String, dynamic> json) {
       id = json['id'];
       qty = json['qty'];
    }
 
     Map<String, dynamic> toJson() {
       final Map<String, dynamic> data = new Map<String, dynamic>();
          data['id'] = this.id;
          data['qty'] = this.qty;
          return data;
        }
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.