exportData method

Future exportData(
  1. SelectModel? selectModel,
  2. bool onlyFiltered,
  3. GroupFilterExp filter,
  4. String textSearch,
  5. TypeSearch typeSearch,
)

Implementation

Future exportData(SelectModel? selectModel, bool onlyFiltered,
    GroupFilterExp filter, String textSearch, TypeSearch typeSearch) async {
  StringBuffer stringBuffer = StringBuffer();
  List<Map<String, dynamic>> data = listAll!;
  if (onlyFiltered) {
    if (!textSearch.isNullOrBlank) {
      data = applyFilterList(typeSearch, listAll!, textSearch);
    }
    data = applyFilters(data, filter);
  }
  if (data.isNotEmpty) {
    for (var key in data.first.keys) {
      stringBuffer
        ..write(key)
        ..write(';');
    }
    stringBuffer.write('\n');
  }
  for (var item in data) {
    for (MapEntry entry in item.entries) {
      Line? line = selectModel?.lines
          .firstWhereOrNull((element) => element.key == entry.key);
      var value = entry.value;
      if (line != null && line.formatData != null) {
        value = line.formatData!
            .formatData(ObjFormatData(data: value, map: item));
      }
      if (decimalSeparatorInCSV != '.' && value is double) {
        value = '$value'.replaceAll('.', decimalSeparatorInCSV);
      }
      stringBuffer
        ..write(value)
        ..write(';');
    }
    stringBuffer.write('\n');
  }
  UtilsFileSelect.saveFileString(stringBuffer.toString(),
      dirComplementar: '${selectModel!.title}',
      fileName: '${DateTime.now().string('dd-MM-yyyy HH-mm-ss')}.csv');
  return;
}