Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read CSV file from device storage in flutter

Tags:

csv

flutter

dart

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?

like image 634
JahidRatul Avatar asked Oct 26 '25 02:10

JahidRatul


1 Answers

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);
   }
 }
like image 70
JahidRatul Avatar answered Oct 27 '25 16:10

JahidRatul