autoParse function

Map<String, dynamic> autoParse (
  1. Map inputData,
  2. Iterable<String> fields
)

Converts the desired fields to their numeric representations, if present.

Implementation

Map<String, dynamic> autoParse(Map inputData, Iterable<String> fields) {
  Map<String, dynamic> data = {};

  for (var key in inputData.keys) {
    if (!fields.contains(key)) {
      data[key.toString()] = inputData[key];
    } else {
      try {
        var n = inputData[key] is num
            ? inputData[key]
            : num.parse(inputData[key].toString());
        data[key.toString()] = n == n.toInt() ? n.toInt() : n;
      } catch (e) {
        // Invalid number, don't pass it
      }
    }
  }

  return data;
}