Home > Blockchain >  Post request in Flutter
Post request in Flutter

Time:03-14

I'm trying to post data to the Laravel server but I end up getting the below message as a response. This makes me wonder if there's something wrong with the way I'm posting the data. What's funny is that the parameters that are meant to be sent to the server prints the values passed in them prior to the call. However, it appears that these parameters don't really have any data stored in them when Posted to the server. Strangely enough, upon trying to do the same from POSTMAN, it appears to work fine.

This is my code:

Future<void> addItems(
      int id,
      int quantity,
      String restaurantId) async {
    print(id);           //prints the value for id
    print(quantity);     //prints the value for quantity
    print(restaurantId); //prints the value for restaurantId
    final url = Uri.parse(baseUrl   'api/auth/cart');
    final response = await http.post(url,
        body: jsonEncode({
          'product_id': id.toString(),
          'restaurant_id': restaurantId,
          'quantity': quantity.toString(),
        }),
        headers: {
          'Authorization': 'Bearer ${network.getToken()}',
          'Accept': 'application/json',
        });
    print(response.body);
  }

This is the widget from which the above function gets triggered:

class ItemDetailsState extends State<ItemDetails> {
  int quantity = 1;

  @override
  Widget build(BuildContext context) {
    final routes =
        ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
    final id = routes["id"];
    final restaurantId = routes['restaurantId'];
    final name = routes['name'];

return Scaffold(
        body: Column(
      ............
      ............
      ............  
        quantity > 0
            ? InkWell(
                onTap: () {
                  Provider.of<CartItemProvider>(context, listen: false)
                      .addItems(id, name, restaurantId);       //This is where the function is getting invoked from
                },
                child: Container(
                  .........
          .........
          .........
              )
    }
}

The backend response:

{"message":"The given data was invalid.","errors":{"product_id":["The product id field is required."],"restaurant_id":["The restaurant id field is required."],"quantity":["The quantity field is required."]}}

Doing the following throws the below error:

 final response = await http.post(url,
        body:{
          'product_id': id.toString(),
          'restaurant_id': restaurantId,
          'quantity': quantity.toString(),
        },
        headers: {
          'Authorization': 'Bearer ${network.getToken()}',
          'Accept': 'application/json',
        }
  );

The error I now get:

( 4472): {
    I/flutter ( 4472):     "message": "Trying to get property 'id' of non-object",
    I/flutter ( 4472):     "exception": "ErrorException",
    I/flutter ( 4472):     "file": "/home3/achievex/public_html/current_work/eatiano/app/Http/Controllers/cartController.php",
    I/flutter ( 4472):     "line": 21,
    I/flutter ( 4472):     "trace": [
    I/flutter ( 4472):         {
    I/flutter ( 4472):             "file": "/home3/achievex/public_html/current_work/eatiano/app/Http/Controllers/cartController.php",
    I/flutter ( 4472):             "line": 21,
    I/flutter ( 4472):             "function": "handleError",
    I/flutter ( 4472):             "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
    I/flutter ( 4472):             "type": "->"
    I/flutter ( 4472):         },

CodePudding user response:

Just remove the jsonEncode from body like this one:

 final response = await http.post(url,
        body:{
          'product_id': id.toString(),
          'restaurant_id': restaurantId,
          'quantity': quantity.toString(),
        },
        headers: {
          'Authorization': 'Bearer ${network.getToken()}',
          'Accept': 'application/json',
        }
  );

CodePudding user response:

 final response = await http.post(url,
    body:{
      'product_id': id.toString(),
      'restaurant_id': restaurantId,
      'quantity': quantity.toString(),
    },
    headers: {
      'Authorization': 'Bearer ${network.getToken()}',
      'Accept': 'application/json',
    }
  );

print(response);
  • Related