how to load csv file in flutter?

7,987

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:

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.

Share:
7,987
Djamila Jada
Author by

Djamila Jada

Updated on December 10, 2022

Comments

  • Djamila Jada
    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
      Austin almost 5 years
    • Djamila Jada
      Djamila Jada almost 5 years
      no not the same i want to load csv file with rootBundle
    • Austin
      Austin almost 5 years
      doesn't the answer in that link use rootBundle?
    • Djamila Jada
      Djamila Jada almost 5 years
      what dose mean my error and how to solve it
    • Djamila Jada
      Djamila Jada almost 5 years
      no not the answer
    • Raouf Rahiche
      Raouf Rahiche almost 5 years
      there is missing space here - asset/thesaurusEn.csv it should be two spaces and not just one
    • Djamila Jada
      Djamila Jada almost 5 years
      no there is no missing space ,bcz when i save it it give me no error
    • Djamila Jada
      Djamila Jada almost 5 years
      just when i copie it it look like that
    • Raouf Rahiche
      Raouf Rahiche almost 5 years
      no, it won't because it's a dart code it's in your pubspect.yaml
    • Raouf Rahiche
      Raouf Rahiche almost 5 years
    • Djamila Jada
      Djamila Jada almost 5 years
      thank you but i still have the same error :(