Dart CSV写作 [英] Dart CSV Writing
问题描述
嘿,所以我并没有真正弄乱它,但是我想知道是否真的有办法(在我从一个永无休止的兔子洞中摔下来之前)在Dart/Flutter中读取和写入CSV文件?我需要写文件,而不必读它们,而且我愿意花很长的时间写这些文件.任何库都可以工作,内置函数甚至更好.任何帮助都将不胜感激!
Hey so I haven't really messed around with it too much, but I was wondering if there was actually a way (before I go down a neverending rabbit hole) to read and write to CSV files in Dart/Flutter? I need to write to the files, not necessarily read them, and I'm willing to go to quite extreme lengths to do so. Any library works, built-in functions are even better. Any and all help is appreciated!
推荐答案
使用 https://pub.dartlang.org/packages/csv .
如果您有需要转换为csv的 List< List< dynamic>>
项,
If you have a List<List<dynamic>>
items that needs to convert into csv,
String csv = const ListToCsvConverter().convert(yourListOfLists);
如果要将csv写入文件,
If you want to write the csv to a file,
/// Write to a file
final directory = await getApplicationDocumentsDirectory();
final pathOfTheFileToWrite = directory.path + "/myCsvFile.csv"
File file = await File(pathOfTheFileToWrite);
file.writeAsString(csv);
此外,如果您想将csv文件直接读取到 list< list< dynamic>>
Also, if you want to read a csv file directly into list<list<dynamic>>
final input = new File('a/csv/file.txt').openRead();
final fields = await input.transform(UTF8.decoder).transform(csvCodec.decoder).toList();
这篇关于Dart CSV写作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!