我正在制作一个加载CSV文件并将其显示为列表视图的应用程序 但是我看不到我的数据,我不知道为什么会这样。 这是csv链接a link!
运行我的应用程序时,我只会看到此内容
相反,我想这样填充空白处
这是代码
import 'package:flutter/material.dart';
import 'package:csv/csv.dart';
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
class TableLayout extends StatefulWidget {
@override
_TableLayoutState createState() => _TableLayoutState();
}
class _TableLayoutState extends State<TableLayout> {
@override
Widget build(BuildContext context) {
List<List<dynamic>> data = [];
loadAsset() async {
final myData = await rootBundle.loadString("asset/dreamss.csv");
List<List<dynamic>> csvTable = CsvToListConverter().convert(myData);
data = csvTable;
print(data);
}
return MaterialApp(
home:Scaffold(
appBar: AppBar(title: Text("dsf"),),
floatingActionButton: FloatingActionButton( onPressed: () async {
await loadAsset();
print(data);
},),
body: Table(
children: data.map((item){
return TableRow(
children: item.map((row){
return Text(row.toString());
}).toList(),
);
}).toList(),
),
),
);
}
}
答案 0 :(得分:1)
您错过了setState。
每当您更改State对象的内部状态时,都要在传递给setState的函数中进行更改。
请查看完整的代码,如下所示:
/app
其打印数据如下:
class TableLayout extends StatefulWidget {
@override
_TableLayoutState createState() => _TableLayoutState();
}
class _TableLayoutState extends State<TableLayout> {
List<List<dynamic>> data = [];
loadAsset() async {
final myData = await rootBundle.loadString("asset/dreamss.csv");
List<List<dynamic>> csvTable = CsvToListConverter().convert(myData);
return csvTable;
}
void load() async{
var newdata = await loadAsset();
setState(() {
data = newdata;
});
print(data);
}
@override
Widget build(BuildContext context) {
return MaterialApp(home: Scaffold(
appBar: AppBar(title: Text("dsf"),),
floatingActionButton: FloatingActionButton( onPressed: load),
body: ListView(
children: <Widget>[
Table(
children: data.map((item){
return TableRow(
children: item.map((row){
return Text(row.toString());
}).toList(),
);
}).toList(),
),
]),
));
}
}