我创建了一个加载CSV文件并将其显示为列表视图的应用程序,我使用了以下示例。https://gist.github.com/Rahiche/9b4b2d3b5c24dddbbe662b58c5a2dcd2
问题是我的List不生成行
I/flutter ( 2158): [[M01, Plastics, 50, NA
I/flutter ( 2158): M02, Plastics, 85, NA
I/flutter ( 2158): M03, Wood, 50, NA
I/flutter ( 2158): M04, Wood, 15, 3
I/flutter ( 2158): M05, Plastics, 50, NA]]
下面是我的代码
class TableLayout extends StatefulWidget {
@override
_TableLayoutState createState() => _TableLayoutState();
}
class _TableLayoutState extends State<TableLayout> {
List<List<dynamic>> data = [];
loadAsset() async {
final myData = await rootBundle.loadString("assets/ford.csv");
List<List<dynamic>> csvTable = CsvToListConverter().convert(myData);
print(csvTable);
data = csvTable;
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: FloatingActionButton(
child: Icon(Icons.refresh),
onPressed: () async {
await loadAsset();
//print(data);
}),
appBar: AppBar(
title: Text("Table Layout and CSV"),
),
body: SingleChildScrollView(
child: Table(
columnWidths: {
0: FixedColumnWidth(100.0),
1: FixedColumnWidth(200.0),
},
border: TableBorder.all(width: 1.0),
children: data.map((item) {
return TableRow(
children: item.map((row) {
return Container(
color:
row.toString().contains("NA") ? Colors.red : Colors.green,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
row.toString(),
style: TextStyle(fontSize: 20.0),
),
),
);
}).toList());
}).toList(),
),
),
);
}
}
和我的ford.csv
M01,Plastics,50,NA
M02,Plastics,85,NA
M03,Wood,50,NA
M04,Wood,15,3
M05,Plastics,50,NA
--- i tried the hints from https://pub.dev/packages/csv#-readme-tab- and from Not viewing Table Layout from a csv in flutter and I have read several csv files but every time i have the same issues.
我到底做错了什么?
请帮助一位新的Flutter研制人员。)
3条答案
按热度按时间vq8itlhq1#
你可以复制粘贴运行下面的完整代码
我在函数
loadAsset()
中添加了setState
我没有遇到列宽问题,如果您仍然有此问题,请尝试添加列2、3或缩小FixedColumnWidth的宽度
代码片段
工作演示
动画GIF没有显示正确的绿色,
所以我把最终结果粘贴到第二张图片中
全码
xpszyzbs2#
这是因为不同的EOL(行结束)字符用于终止文件中的一行。即一些程序使用'\r\n'而其他'\n'。
因此,要解决这个问题,你必须考虑到。即我正在使用csv包在windows操作系统上,而从csv文件阅读,你应该指定eol参数
CsvToListConverter()
的convert
方法。z8dt9xmd3#
fast_csv解析器用于解析CSV数据。
它比csv解析器快2.9 - 4.7倍。
如果您需要解析大量的数据,那么它会更有效率。
行尾为
\n
、\r\n
或\r
(无需配置)。输出: