我在扑朔迷离中使用了一个名为curved_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;
}