mercredi 19 mai 2021

How to do a get call in Flutter web and export the response into csv file with specific headers and filtered data

This is something I got off the internet. It downloads an excel sheet but the data is not correct. And I want it in CSV format, This is in excel.

Future<List> fetchResults(http.Client client) async { final url = "https://123/xyz/abc/";

  final response = await http.get(
    url,
    headers: <String, String>{
  
    },
  );

  if (response.statusCode == 200) {
    // Use the compute function to run parseResults in a separate isolate
    return compute(parseResults, response.body);
    // print(response.body);
  } else {
    throw Exception();
  }
}

    List<Result> parseResults(String responseBody) {
  final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
  return parsed.map<Result>((json) => Result.fromJson(json)).toList();
}

onPressed: () async {
                                      Excel execl = Excel.createExcel();

                                      List<dynamic> list =
                                          await fetchResults(http.Client(), '');
                                      print(list);

                                      for (int i = 0; i < list.length; i++)
                                        // execl.insertRowIterables("Sheet1", ['a', i], i);
                                        execl.insertRowIterables(
                                            "Sheet1", list, i);

                                      List<int> sheets = await execl.encode();
                                      Uint8List data =
                                          Uint8List.fromList(sheets);
                                      MimeType type = MimeType.MICROSOFTEXCEL;
                                      if (!kIsWeb) {}
                                      String val =
                                          await FileSaver.instance.saveFile(
                                        textEditingController?.text == ""
                                            ? "File"
                                            : textEditingController.text,
                                        data,
                                        "xlsx",
                                        mimeType: type,
                                      );
                                      print(val);
                                    },



Aucun commentaire:

Enregistrer un commentaire