我有多个标签。例如,我有两个选项卡,选项卡1和选项卡2。在选项卡1,有一个future
来获取我的数据,然后我转到选项卡2,然后再次回到选项卡1,但是future
再次运行。好吧,我正在尝试使用
bool get wantKeepAlive => true;
这是我的剧本
Future<List<HotProducts>> _loadHotProducts;
@override
void initState() {
_loadHotProducts = fetchHotProducts(http.Client());
super.initState();
}
@override
Widget build(BuildContext context) {
super.build(context);
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
------------
}
Widget populateHotProduct() {
return FutureBuilder<List<HotProducts>>(
future:this._loadHotProducts,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting &&
snapshot.hasError == false) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.connectionState == ConnectionState.waiting &&
snapshot.hasError == true) {
return error_();
} else if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasError == true) {
return error_();
} else if(snapshot.hasError == true){
return error_();
} else{
return HotProductsLists(hotlist: snapshot.data);
}
},
);
}
Future<List<HotProducts>> fetchHotProducts(http.Client client) async {
final response = await http.post(Configuration.url + "api/getHotProducts");
if (response.statusCode < 200 || response.statusCode > 300) {
throw new Exception('Failed to fetch data');
} else {
return compute(parseHotProducts, response.body);
}
}
static List<HotProducts> parseHotProducts(String responseBody) {
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<HotProducts>((json) => HotProducts.fromJson(json)).toList();
}
一件事,当我尝试使用
打开另一个屏幕时 Navigator.push(context, new MaterialPageRoute(
builder: (BuildContext context) {
return ProductDetailPage(data: newlist[index]);
}
));
回到标签1,我的未来不再运行。
那么,我要如何解决我的问题,谢谢。