flutter - return new futureBuilder 在屏幕尺寸更改时不断向服务器请求
问题描述
我正在构建一个应用程序,它有一个从服务器请求数据的底部栏,当我更改屏幕大小时,它再次向服务器请求
这是我的底部栏小部件
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();
},
);
}
无论如何,我可以问一次吗?
解决方案
将 future 放入 init() 状态。我希望它只会运行一次。PS:不要把它放在 setState() 中,它会在每次屏幕重新生成时运行。
推荐阅读
- android - 如何使用下拉列表制作材料芯片
- google-apps-script - 用户可通过按钮将每分钟触发器安装到独立脚本
- html - 我的网站没有正确加载。连接到某个网络时显示 404 错误,
- rust - 专用人造丝池提交工作并异步等待完成
- c++ - uWebSockets 客户端
- html - 如何在css中为图片制作三角形底部或做出反应
- java - 用于 apche poi docx 的 HTML 解析器,用于将 html 插入段落
- hadoop - 如果我通过 Oozie(从 splunk 下载数据)获取主类 [org.apache.oozie.action.hadoop.ShellMain],退出代码 [1]
- unicode - 无法显示 HEBREW POINT QAMATS QATAN 的 unicode (\u05c7)
- c++ - 向量反序列化的 YAML BadConversion 异常