Tuesday, August 30, 2022

[FIXED] How to read CSV file from device storage in flutter

Issue

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?


Solution

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);
   }
 }


Answered By - JahidRatul
Answer Checked By - Marilyn (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.