Skip to content

Instantly share code, notes, and snippets.

@NemesisX1
Last active June 15, 2022 16:58
Show Gist options
  • Select an option

  • Save NemesisX1/eeed94a958ca24abcd540f01861026ed to your computer and use it in GitHub Desktop.

Select an option

Save NemesisX1/eeed94a958ca24abcd540f01861026ed to your computer and use it in GitHub Desktop.
A simple way to retrieve CSV and read its contents on Flutter Web
// Note: This is a simple way to read CSV file with Flutter Web
// You must add those packages to make the code work:
// - csv
// - file_picker_web
Future<void> importCSV() async {
// Using the file_picker_package to get the file
final csvFile = await FilePicker.platform.pickFiles(
allowMultiple: false,
withData: true,
type: FileType.custom,
allowedExtensions: ['csv'],
);
if (csvFile != null) {
//decode bytes back to utf8
final bytes = utf8.decode(csvFile.files.first.bytes!);
//from the csv plugin
List<List<dynamic>> rowsAsListOfValues =
const CsvToListConverter().convert(bytes);
// Let us display the CSV file content
for (int i = 0; i < rowsAsListOfValues.length; i++) {
for (int j = 0; j < rowsAsListOfValues.elementAt(i).length; j++) {
log("${rowsAsListOfValues.elementAt(i).elementAt(0)}");
log(rowsAsListOfValues.elementAt(i).elementAt(1).toString());
log(rowsAsListOfValues.elementAt(i).elementAt(2).toString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment