Unable to load CSV file

995 views
Skip to first unread message

Harish Challa

unread,
Mar 1, 2019, 12:34:03 AM3/1/19
to Flutter Dev
import 'package:flutter/services.dart' show rootBundle;
import 'dart:io';
import 'dart:convert';


loadAsset() async {
List<List<dynamic>> data = [];
List<String> fileData = [];
final myData = await File("assets/authentic.csv");
//final myData = await rootBundle.loadString('assets/authentic.json');
print(myData);
/*fileData = myData.split(",");
print("After splitting $fileData");*/
Stream<List> inputStream = myData.openRead();
inputStream.transform(utf8.decoder).transform(new LineSplitter()).listen(
(String line)
{

List row = line.split(",");
int QRCode = row[0];
String ProductName = row[1];
String ManufacturerName = row[2];
String MfdDate = row[3];
String ExpDate = row[4];
int Quantity = row[5];

print("Processed data : $QRCode, $ManufacturerName, $MfdDate");
},
onDone: () { print('File is now closed.'); },
onError: (e) { print(e.toString()); });
}





pubspec.yaml

assets:
- assets/authentic.csv

Harish Challa

unread,
Mar 1, 2019, 12:35:35 AM3/1/19
to Flutter Dev
Error is the folowing
FileSystemException: Cannot open file, path = 'assets/authentic.csv' (OS Error: No such file or directory, errno = 2)

do...@descombaz.com

unread,
Mar 1, 2019, 2:25:38 AM3/1/19
to Flutter Dev
I believe you need to use the path provider: path_provider: ^0.5.0+1 (currently)

Example: (stolen from somewhere on the Flutter site)

import 'package:path_provider/path_provider.dart';

Future<String> get localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}

Future<File> getFile(String name) async {
final path = await localPath;
return File('$path/$name');
}


Then, you would call getFile("assets/authenticate.csv") ... (I don't think you need the `/` before assets)

Harish Challa

unread,
Mar 1, 2019, 7:24:21 AM3/1/19
to Flutter Dev
Thanks for your help
but i am not loading CSV file from loacl path,i am loading from project file which has included in pubspec.yaml file also.

Any help is appreciated,thanks.


On Friday, 1 March 2019 11:04:03 UTC+5:30, Harish Challa wrote:

Hugo van Galen

unread,
Mar 7, 2019, 9:32:18 AM3/7/19
to Harish Challa, Flutter Dev
I see why there is some confusion because you did not say what you want the code to do.

If that is not mentioned, people will try to deduce that from context, and your code *does* imply that you want to read a local file: you commented out the code that loads from the asset bundle, and put code in there to load a local file. (In fact, it looks like you want the code to do what is being done in some of the commented-out code, so that adds to the confusion.)

If your intent is to load from the asset bundle, then you need to call "rootBundle.loadString" and then you can get to the file contents.

Good luck!

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages