how to load csv file in flutter?
Solution 1
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 [[],[],[],[]]
.
Solution 2
Your code should be working. I've checked it and it's working fine.
Here is the output:
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");
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:
But this is still executable, if you run the app this is the output:
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:
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.
Djamila Jada
Updated on December 10, 2022Comments
-
Djamila Jada over 1 year
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)
-
Austin almost 5 yearspossible duplicate of: stackoverflow.com/questions/52986359/…
-
Djamila Jada almost 5 yearsno not the same i want to load csv file with rootBundle
-
Austin almost 5 yearsdoesn't the answer in that link use rootBundle?
-
Djamila Jada almost 5 yearswhat dose mean my error and how to solve it
-
Djamila Jada almost 5 yearsno not the answer
-
Raouf Rahiche almost 5 yearsthere is missing space here - asset/thesaurusEn.csv it should be two spaces and not just one
-
Djamila Jada almost 5 yearsno there is no missing space ,bcz when i save it it give me no error
-
Djamila Jada almost 5 yearsjust when i copie it it look like that
-
Raouf Rahiche almost 5 yearsno, it won't because it's a dart code it's in your pubspect.yaml
-
Raouf Rahiche almost 5 yearscheck out this example gist.github.com/Rahiche/4c74680b0d944511f8b8e5622ec21154
-
Djamila Jada almost 5 yearsthank you but i still have the same error :(
-