Flutter return new futureBuilder不断向服务器请求屏幕大小更改
我正在构建一个应用程序,它有一个底部栏,可以从服务器请求数据,当我更改屏幕大小时,它会再次向服务器请求数据 这是我的底部工具栏小部件Flutter return new futureBuilder不断向服务器请求屏幕大小更改,flutter,dart,Flutter,Dart,我正在构建一个应用程序,它有一个底部栏,可以从服务器请求数据,当我更改屏幕大小时,它会再次向服务器请求数据 这是我的底部工具栏小部件 Widget bottomNavigationBar() { return new BottomAppBar( color: const Color(0xFF1E90FF), child: new Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, childr
Widget bottomNavigationBar() {
return new BottomAppBar(
color: const Color(0xFF1E90FF),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children:[
Container(
padding: EdgeInsets.only(left: 20),
child: futureBuilderWidget(setVersion())
),
Container(
child: Text(
DateFormat('yyyy-MM-dd hh:mm:ss').format(DateTime.now()),
style: TextStyle( fontSize: 16, color: const Color(0xffebebeb))
),
),
],
),
);
}
这是我的futureBuilder小部件
Widget futureBuilderWidget(_future) {
return new FutureBuilder<dynamic>(
future: _future,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Text(
snapshot.error.toString(),
style: TextStyle(fontSize: 16, color: const Color(0xffebebeb)),
);
} else if (snapshot.hasData) {
return Text(snapshot.data,
style: TextStyle(fontSize: 16, color: const Color(0xffebebeb)),
);
}
}
return CircularProgressIndicator();
},
);
}
Widget futureBuilderWidget(\u future){
返回新的FutureBuilder(
未来:未来,
生成器:(BuildContext上下文,异步快照){
if(snapshot.connectionState==connectionState.done){
if(snapshot.hasError){
返回文本(
snapshot.error.toString(),
样式:TextStyle(fontSize:16,颜色:常量颜色(0xFFEBEB)),
);
}else if(snapshot.hasData){
返回文本(snapshot.data,
样式:TextStyle(fontSize:16,颜色:常量颜色(0xFFEBEB)),
);
}
}
返回循环ProgressIndicator();
},
);
}
还有什么我可以只问一次的吗?将future放在init()状态中。我希望它只运行一次。PS:不要将它放在setState()中,它将在每次屏幕重新生成时运行。我假设
\u future
包含HTTP请求。如文件中所述,未来
应提前获取
未来必须在更早的时候获得,例如在State.initState、State.didUpdateWidget或State.didChangeDependencies期间。在构造FutureBuilder时,不能在State.build或stateWidget.build方法调用期间创建它。如果future与FutureBuilder同时创建,则每次重建FutureBuilder的父级时,异步任务都将重新启动
在这里,您可以在initState()
上运行请求,并使用setState()
更新Future\u Future
以触发小部件重建,或者使用包仅运行一次请求
final asyncmomeizer\u memomeizer=asyncmomeizer();
未来{
return _memorizer.runOnce(()异步{
//做请求
});
}
您是否尝试使用布尔值,以便验证并存储底部栏是否已加载?是的,我尝试过,但我打破了底部栏,它进入全屏,没有显示任何正确的内容,但您也可以在启动应用程序时存储服务器上的数据。