gpt4 book ai didi

在构建中无限调用 Flutter 函数

转载 作者:IT王子 更新时间:2023-10-29 06:53:09 25 4
gpt4 key购买 nike

我正在制作一个加载 CSV 并在屏幕上显示表格的应用程序,但加载函数在构建状态下被无限调用任何人都知道如何修复它我只想调用一次但我的代码调用了很多次次。

控制台截图如下: a busy cat

代码如下:

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> {

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("am i still being called called ");
}

@override
Widget build(BuildContext context) {

load();

return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Apps"),),
//floatingActionButton: FloatingActionButton( onPressed: load,child: Icon(Icons.refresh),),
body: ListView(
children: <Widget>[
Container(margin: EdgeInsets.only(top: 20.0),),
Table(
border: TableBorder.all(width: 1.0,color: Colors.black),
children: data.map((item){
return TableRow(
children: item.map((row){
return Text(row.toString(),style: TextStyle(fontSize: 20.0,fontWeight: FontWeight.w900),);
}).toList(),
);
}).toList(),
),
]),
));
}
}

最佳答案

这是解决方案。

@override
void initState() {
super.initState();
load(); // use it here
}


@override
Widget build(BuildContext context) {
return MaterialApp(...); // no need to call initState() here
}

关于在构建中无限调用 Flutter 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56312092/

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