【发布时间】:2019-12-06 09:22:29
【问题描述】:
在颤振应用程序中,我试图通过打开一个流来一次读取一个大的 csv 文件。问题是当我尝试收听流时,执行只是跳过该代码块并且程序结束。
我正在打开的文件位于我的资产文件夹中,并且我已通过编程方式确认它在打开流之前确实存在。更改打开流的文件无济于事,同样的问题仍然存在。我还尝试改变我听流的方式,遵循 Darts 官方文档提供的不同方法(该代码被注释掉),但结果还是一样的。 The assets have been declared in the pubspec.yaml。当我更改代码以将文件作为字符串读取时,程序运行良好,但我想使用流,因为文件太大,为它创建字符串对象需要大量时间和内存。
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);});
});
}
【问题讨论】:
-
从控制台启动
flutter run ...并在应用崩溃时粘贴输出 -
可以发
trainDigits()的完整代码吗? (添加listen之后的内容)