flutter - 弯曲的导航包在点击时重建页面
问题描述
我在flutter中使用了一个名为curd_navigation_bar的包。我遇到的唯一问题是,每次我点击一个按钮时,该页面都会重新构建。我正在主页上获取一些数据,并且在获取我的数据时有一个加载微调器。但是,每次我转到不同的选项卡,然后返回主页时,都会再次获取数据,因此每次返回时都必须重新加载,因此使用了微调器。有没有办法只获取一次数据?
main.dart(我从其他文件导入了所有其他小部件):
Widget build(BuildContext context) {
return Scaffold(
body: _currentIndex == 0
? Home()
: _currentIndex == 1
? Data()
: _currentIndex == 2 ? Precautions() : Settings(),
bottomNavigationBar: CurvedNavigationBar(
height: 60,
animationDuration: Duration(milliseconds: 250),
backgroundColor: accentColor,
color: mainColor,
buttonBackgroundColor: mainColor,
items: [
NavBarItem(Icons.home, _currentIndex == 0 ? null : 'Home'),
NavBarItem(Icons.data_usage, _currentIndex == 1 ? null : 'Data'),
NavBarItem(
Icons.favorite_border, _currentIndex == 2 ? null : 'Precautions'),
NavBarItem(Icons.settings, _currentIndex == 3 ? null : 'Settings'),
],
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
),
);
}
}
home.dart 中的代码,它使用 futurebuilder 来获取数据:
Map<String, dynamic> local;
Future<Map<String, dynamic>> fetchLocal() async {
Map<String, dynamic> result;
String url = 'api that i am using to fetch data.com';
var responseLocal = await http.get(url);
var convertedLocal = jsonDecode(responseLocal.body);
result = convertedLocal[convertedLocal.length - 1];
local = result;
return result;
}
解决方案
您可以使用bdnavs。它每次都更新数据。
推荐阅读
- java - 使用 java swing 的新行 JLabel
- sql - 在 apex 中不断收到无效的标识符和不正确的数据类型
- python - 如何解决“DictReader”对象不可下标错误?
- kubernetes - 在 openshift/kubernetes 中打印当前命名空间的简单命令或环境变量
- python - 具有依赖项初始化错误的自定义包:ModuleNotFoundError 或 ImportError
- python - Keras 从多个标签的目录流出
- c - C 原语可以比 void* 宽吗?
- rust - 为什么对于整数类型,`sum += x` 与 `sum += *x` 的工作方式相同?这是自动取消引用吗?
- python - 下载 cloudtrail 事件
- javascript - 保持来自不同父 div 的按钮同时处于活动状态