防止在切换选项卡后再次颤动

时间:2019-07-18 13:32:02

标签: flutter dart

我有多个标签。例如,我有两个选项卡,选项卡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,我的未来不再运行。

那么,我要如何解决我的问题,谢谢。

0 个答案:

没有答案