2

I want to use a csv file in my flutter app, but I have an exception. How to solve it please?

my data.csv 

id,symbol,open,high,low,close,volume,exchange,timestamp,date,update_date_time
33668191,C,16.2,16.3042,16.2,16.3042,477600,NYSE,221115600,1977-01-03,8/26/2013 19:35:35
33668192,C,16.3042,16.3542,16.25,16.3542,340800,NYSE,221202000,1977-01-04,8/26/2013 19:35:35
33668670,C,15.3667,15.5208,15.3667,15.4708,76800,NYSE,280731600,1978-11-24,8/26/2013 19:35:36

I added assets in my pubspec.yaml

assets: - asset/thesaurusEn.csv - asset/data.csv

List<List<dynamic>> data =[];

loadAsset() async{

  final myData = await  rootBundle.loadString('asset/data.csv');
  print(myData);

}

Exception has occurred. FlutterError (Unable to load asset: asset/data.csv)

Petro
  • 3,484
  • 3
  • 32
  • 59
Djamila Jada
  • 141
  • 2
  • 4
  • 11

4 Answers4

2

You can use the package import 'package:csv/csv.dart';
Inside you have a method called CsvToListConverter(), using this you can convert your CSV data to a nice list.

import 'package:csv/csv.dart';
...
List<List<dynamic>> rowsAsListOfValues = const CsvToListConverter().convert(data);

Data will be in [[],[],[],[]].

Gerald H
  • 454
  • 4
  • 13
1

Your code should be working. I've checked it and it's working fine.

Here is the output:

enter image description here

I've tried to replicate your issue and it needs to do something with the naming convention of your "asset" folder, the declaration of asset/ in the pubspec.yaml file, and lastly in your code final myData = await rootBundle.loadString('asset/data.csv');

If you have misspelled in either one of those, here's what will happen.

Scenario 1:

Folder created is "assets"

pubspec.yaml

assets:
 - assets/

main.dart

final myData = await rootBundle.loadString("asset/data.csv");

enter image description here

Scenario 2:

Folder created is "assets"

pubspec.yaml

assets:
 - asset/

main.dart

final myData = await rootBundle.loadString("assets/data.csv");

The moment you've save your pubspec.yaml, you will get an error like this:

enter image description here

But this is still executable, if you run the app this is the output:

enter image description here

Scenario 3:

Folder created is "asset"

pubspec.yaml

assets:
 - assets/

main.dart

final myData = await rootBundle.loadString("assets/data.csv");

Same error with Scenario 1 and 2:

enter image description here

Since you got an error:

Exception has occurred. FlutterError (Unable to load asset: asset/data.csv)

Check your folder, and pubspec.yaml if it is all named correctly ("asset").

I suggest making it all to "assets" as it was defined in the examples in documentation.

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65
0

Something simple to try is to Stop the app entirely on your IDE and then Run it again instead of using Hot Restart/Reload. I was unable to load my CSV file until I did that. Woops!

Adam Bridges
  • 306
  • 3
  • 7
0

Are you trying to load your data in the main() function, before you call runApp()?

In that case make sure you call WidgetsFlutterBinding.ensureInitialized() before you try to read the data.

Here's an example:

void main() async {
    WidgetsFlutterBinding.ensureInitialized(); // <---- Crucial
    final csvData = await readCsvData();
    runApp(MyApp(csvData));
}
Tom O
  • 2,495
  • 2
  • 19
  • 23