prepareNavsArray function

List<Map<String, String>> prepareNavsArray(
  1. String txt
)

Given a text fetched from the NAV URL, this function returns an array of objects. Each object represents a mutual fund and contains its details. @param {String} txt - the text fetched from the NAV URL @returns {List<Map<String, String>>> an array of objects, each representing a mutual fund

Implementation

List<Map<String, String>> prepareNavsArray(String txt) {
  // Removes blank lines from the given text.
  String removeBlankLines(String txt) {
    return txt
        .replaceAll(RegExp(r'(\n{2,})'), '\n')
        .replaceAll(RegExp(r'(\r)'), '');
  }

  bool isDataRow(String row, List<String> headers) {
    return row.split(";").length == headers.length;
  }

  final rows =
      removeBlankLines(txt).split("\n").where((el) => el.length > 1).toList();
  final headers = rows.removeAt(0).split(";");

  final List<Map<String, String>> arr = [];
  String schemeType = "";
  String amcName = "";

  for (var i = 0; i < rows.length; ++i) {
    final row = rows[i];
    // check if the row is a data row
    if (isDataRow(row, headers)) {
      final attr = row.split(";");
      final data = <String, String>{};
      for (var j = 0; j < attr.length; ++j) {
        data[headers[j]] = attr[j];
      }
      data["AMC Name"] = amcName;
      data["Scheme Type"] = schemeType;
      arr.add(data);
    } else if (isDataRow(rows[i + 1], headers)) {
      // else this will be the AMC Name Row
      amcName = row; // Set AMC Name
    } else {
      // else this will be the Scheme Type Row
      schemeType = row; // Set Scheme Type
    }
  }
  return arr;
}