Flutter web: Convert CSV to list for

1.1k Views Asked by At

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

1

There are 1 best solutions below

1
On

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

employeeData = CsvToListConverter(eol: "\r\n",fieldDelimiter: ",").convert(bytes);