【发布时间】:2020-06-18 08:19:38
【问题描述】:
我有一个小部件,它正在从我的其他类之一(booruHandler)获取数据,我正在使用未来构建器的未来搜索功能,但我需要多次调用它,但只有在未来构建器有完成后,我在另一个小部件中使用类似的未来构建器,并在发生操作时使用 setState 再次调用它以增加 widget.pageNum。有没有办法让 setState 在未来的构建者完成构建时被调用?我尝试将 setState 放在 futurebuilder 中,如果 connectionState 完成则调用它,但这不起作用
class SnatcherProgressPage extends StatefulWidget {
String tags,amount,timeout;
int pageNum=0;
int count=0;
SnatcherProgressPage(this.tags,this.amount,this.timeout);
@override
_SnatcherProgressPageState createState() => _SnatcherProgressPageState();
}
class _SnatcherProgressPageState extends State<SnatcherProgressPage> {
static int limit, count;
@override
void initState() {
// TODO: implement initState
super.initState();
if (int.parse(widget.amount) <= 100){limit = int.parse(widget.amount);} else {limit = 100;}
}
BooruHandler booruHandler = new GelbooruHandler("https://gelbooru.com", limit);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Snatching"),
),
body: FutureBuilder(
future: booruHandler.Search(widget.tags,widget.pageNum),
builder: (context, AsyncSnapshot snapshot){
switch(snapshot.connectionState){
case ConnectionState.active:
return Text("Snatching");
break;
case ConnectionState.done:
if (snapshot.data.length < int.parse(widget.amount)){
// Inc pagenum to get more data
// Call the writer function on all of the data
}
return Text(snapshot.data.length.toString());
break;
case ConnectionState.waiting:
return CircularProgressIndicator();
break;
case ConnectionState.none:
return Text("hmmmmmm");
break;
}
return Text("hmmmmmm");
},
),
);
}
}
【问题讨论】: