Search code examples
flutterdartflutter-dependenciesflutter-test

Flutter post api sending multiple values with same name parameter


How multiple data can be send with same name parameter in post api in flutter. In php we use parameter[] and then we can send multiple data but I am new to flutter so I want to know how it can be done. thanx

Example Image: https://i.sstatic.net/X7jL3.png


Solution

  • I hope you solved this already but in case you didn't I faced the same problem and found a workaround I had a similar API : Here

    the solution is that I added a counter variable inside ' [] ' while creating the request body map.

    ex: products[$counter]

    so it will give each key in the body map a different name like this

     final Map<String, dynamic> body = Map<String, dynamic>();
    
    if (this.products != null) {
      for (int i = 0; i < products.length; i++) {
        body['products[$i]'] = json.encode(products[i]);
      }
    

    and yet it will still be recognized by the server as same name parameter.