'Passing a String List in a SOAP request in Flutter

I am making a SOAP Request and this is how my request should be sent:

  <id></id>
  <fieldList>
    <string>string</string>
    <string>string</string>
  </fieldList>

This is how I have built my envelope:

final int id = 21;
List<String> fieldList = new List<String>();

  fieldList = [
    "pinNumber:PIN0000074",
    "dispatchArrivedTime:13.05",
    "towedStatus:C"
  ];

var envelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    "<soap:Envelope "
    "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
    "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
    "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
    "<soap:Body>"
    "<update xmlns=\"http://example.com/\">"
    "<id>${id}</id>"
    "<fieldList>${fieldList}</fieldList>"
    "</update>"
    "</soap:Body>"
    "</soap:Envelope>";

    final response = await http.post(
    'http://example.com/vc/ws/towedvehicletable.asmx',
    headers: {
      "Content-Type": "text/xml; charset=utf-8",
      "SOAPAction": "http://example.com/update",
      "Host": "example.com"
      //"Accept": "text/xml"
    },
    body: envelope);

However, this approach does not work. It would be really helpful if somebody could show me how to pass a String List into my request. I am using Flutter and Dart. Thanks



Solution 1:[1]

Just map list of strings and then join it:

  List<String> fieldList = ['test1', 'test2'];
  final xmlValues = fieldList.map((v) => '<string>$v</string>').join();
  print(xmlValues);

prints:

<string>test1</string><string>test2</string>

There's also a package for working with XML. It allows you to both parse and construct XML documents.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1