gpt4 book ai didi

flutter - 不从 flutter 中的 csv 查看表格布局

转载 作者:IT王子 更新时间:2023-10-29 07:19:31 26 4
gpt4 key购买 nike

我正在制作一个加载 CSV 文件并将其显示为 ListView 的应用程序但是我看不到我的数据,我不知道为什么会这样。这是 csv 链接 a link !

当我运行我的应用程序时,我只看到这个 image

相反,我想像这样填充空白 image

这是代码

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(),
),
),

);
}

}

最佳答案

您错过了 setState .
每当您更改 State 对象的内部状态时,请在传递给 setState 的函数中进行更改。 .
完整代码如下:

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(),
),
]),
));
}
}

打印数据如下:

I/flutter ( 9677): [[S.no, Dream, Direct, House, Ending], [1, Quarrel between husband and wife, 03, 08, 13, 37, 40, 73, , 3], [2, Erotic dream, 17, 40, 53, 59, 60, 83, , ], [3, Bathing in the open, 08, 18, 28, 48, 78, 98, , 8], [4, Travelling, 08, 14, 18, 52, 64, 68, 74, 78, 98, , 8], [5, Travelling in an aeroplane, 23, 43, 53, 63, 68, 73, 83, 93, , 3], [6, Taking a walk, , , 0,1], [7, Studying, , , 5], [8, Corpse, , 9, 9], [9, Playing, 00, 27, 40, 50, 57, 60, , ], [10, Talking over the phone, 98, 96, 94, , ], [11, Eating, 01, 02, 05, 15, 16, 45, 75, 85, 95, , ], [12, Breast Feeding, 02, 03, 05, 12, 20, 52, 53, , ], [13, Male, 6, 6, ], [14, Female, 5, 5, ], [15, Child, 2,3, 2,3, ], [16, Police, 7, 87, 8, , ], [17, Wild Pig, 46, , ], [18, Snake or ilea fish, 09, 17, 37, 57, 77, 99, , 7], [19, Cow, goat or buffalo, 12, 18, 19, 22, 24, 34, 42, 54, 72, 74, 84, 94, 97, , ], [20, Cow, 4, 4, ], [21, Tiger, 9, 9, ], [22, Dog, 4,5,6, 4, ], [23, Horse, 8, 8, ], [24, Bird, 2, 2, ], [25, Elephant, 9, 9, ], [26, Snail, 0, 0, 0], [27, Turtl

请引用我的截图如下:
enter image description here

关于flutter - 不从 flutter 中的 csv 查看表格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56306335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com