3

There is plenty example of CSV to list for flutter apps, I'm facing issues with flutter web and the conversion of the file.

Some CSV aren't working as desired, I guess that it's a formatting issue Here is the converting function when a user upload a file

Future _openFileExplorer() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
        allowMultiple: false,
        withData: true,
        type: FileType.custom,
        allowedExtensions: ['csv']);
    if (result != null) {
      //decode bytes back to utf8
      final bytes = utf8.decode((result.files.first.bytes)!.toList());
      setState(() {
        //from the csv plugin
        employeeData = CsvToListConverter().convert(bytes);
      });
    }
  }

Working CSV file conversion

Working CSV file conversion,

Not working csv file conversion, the structure isn't the same, it's a big chunck

Not working csv file conversion, the structure isn't the same, it's a big chunck,

I did a complete repo of the project if you want to try, added some photos in the README,

https://github.com/valentincx/csv_to_list_for_web

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Judas B
  • 33
  • 6

1 Answers1

3

You should add eol: "\r\n",fieldDelimiter: "," to CsvToListConverter(). your code will be like this:

employeeData = CsvToListConverter(eol: "\r\n",fieldDelimiter: ",").convert(bytes);
  • Added the code, it didn't change much for my case but thanks for the response! Some csv are formatted differently, as you can see in the git repo `mock_data.csv` still doesn't work with this code. I decided to show an error when it's not working and specify that the user should send a specific format. – Judas B Feb 03 '22 at 13:54