How to read CSV file from device storage in flutter

5,504

At first import file_picker and CSV package from dart packages. Than define the method pickFile() as i have given below. it will pick the file from device storage and after selection it will print data. pickFile() funtion shoulb be called to get result.

import 'package:file_picker/file_picker.dart';
import 'package:csv/csv.dart';
import 'dart:convert' show utf8;

pickFile() async {
   FilePickerResult result = await FilePicker.platform.pickFiles();
   if (result != null) {
     PlatformFile file = result.files.first;

     final input = new File(file.path).openRead();
     final fields = await input
         .transform(utf8.decoder)
         .transform(new CsvToListConverter())
         .toList();

     print(fields);
   }
 }
Share:
5,504
JahidRatul
Author by

JahidRatul

I am a Software Developer interested to learning frontend and backend mobile apps developement both Android & iOS Platform .

Updated on December 24, 2022

Comments

  • JahidRatul
    JahidRatul over 1 year

    I want to import data into firebase database from a CSV file in flutter. So I pick .CSV file from device using file picker. Now how can I read data from that file?