How to serialize html form in dart as a string for submission How to serialize html form in dart as a string for submission dart dart

How to serialize html form in dart as a string for submission


I came up with my own simple solution that might not work in all cases (but for me it is workikng). The procedure is this:

  1. First we need to extract all input or select element names and values from the form into Dart's Map, so the element name will be the key and value the value (e.g. {'single': 'Single2'}).
  2. Then we will loop through this Map and manually create the resulting string.

The code might look something like this:

FormElement form = querySelector('#my-form'); // To select the formMap data = {};// Form elements to extract {name: value} fromfinal formElementSelectors = "select, input";form.querySelectorAll(formElementSelectors).forEach((SelectElement el) {  data[el.name] = el.value; });var parameters = "";for (var key in data.keys) {  if (parameters.isNotEmpty) {    parameters += "&";  }  parameters += '$key=${data[key]}';}

Parameters should now contain all the {name: value} pairs from the specified form.


I haven't seen anything like that yet.

In this example Seth Ladd uses Polymers template to assign the form field values to a class which get's serialized.