1

In a flutter app, I'm trying to read a large csv file one line at a time by opening a stream on it. The issue is that when i try to listen to the stream the execution just skips over that code block and the program ends.

The file I'm opening is located in my assets folder and I've confirmed programmatically that it does exist before opening the stream. Changing the file the stream is opened on doesn't help, the same problem persists. I've also tried to change the way i listen to the stream, following different methods provided by Darts official documentation (that code is commented out) but the outcome is again the same. The assets have been declared in the pubspec.yaml. When i change the code to read the file as a String the program works perfectly but I want to use a stream because the file is so massive that creating a String object for it would take a large amount of time and memory.

void trainDigitsStream() async{
  List<List<List>> filters = createRandomFilter(4, 4, 1, -1, 1);
  List flattened= new List<double>();
  File file = new File("assets/digit_train_data.csv");
  if(file.existsSync())print("EXISTS!");  
  Stream<List<int>> stream = file.openRead();
  Stream lines = utf8.decoder.bind(stream).transform(LineSplitter());
  /*
  try{
    await for (var line in lines){
      print(line);
    }
    print("file ended");
  }catch(e){
    print(e);
  }
  */
  lines.listen((data){//code exits here, execution never reaches next line
    String line = data.toString();
    List<List> instance = new List<List<int>>();
    List x = new List<int>();
    int i = 0;        
    line.split(',').forEach((d){
      x.add(int.parse(d));
      i++;
      if(i == 28){
        instance.add(x);
        x = new List<int>();
        i = 0;
      }
    });
    List<List<List>> kernels = new List<List<List<double>>>();
    List<List> pools = new List<List>();
    filters.forEach((f){kernels.add(convo.applyFilter(instance, f, 0));});
    kernels.forEach((k){pools.add(pool.maxPool(k, 2));});
    pools.forEach((p){flattened.addAll(p);});
  }); 
}

1 Answers1

0

It's hard without further information, It would be better if you can post more information.
So I guess the problem should be , please check the following two steps.
1. Register the assets folder in pubspec.yaml

flutter:
  assets:
    - assets/digit_train_data.csv

2. You need to use rootBundle to access this csv file, reference document https://flutter.dev/docs/development/ui/assets-and-images

    import 'dart:async' show Future;
    import 'package:flutter/services.dart' show rootBundle;

    Future<String> loadAsset() async {
      return await rootBundle.loadString('assets/digit_train_data.csv');
    }

similar question here Flutter - Read text file from assets

chunhunghan
  • 51,087
  • 5
  • 102
  • 120