Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 防止颤振切换标签后再次运行_Flutter_Dart - Fatal编程技术网

Flutter 防止颤振切换标签后再次运行

Flutter 防止颤振切换标签后再次运行,flutter,dart,Flutter,Dart,我有多个标签。例如,我有两个选项卡,选项卡1和选项卡2。在选项卡1中有一个future获取我的数据,然后我转到选项卡2,再回到选项卡1,但是future再次运行。嗯,我想用 bool get wantKeepAlive => true; 这是我的剧本 Future<List<HotProducts>> _loadHotProducts; @override void initState() { _loadHotProducts = fetch

我有多个标签。例如,我有两个选项卡,选项卡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();
  }
回到表1,我的未来不再运行

因此,我如何解决我的问题,提前谢谢。

请参阅和。希望有帮助。请参考和。希望能有帮助。
 Navigator.push(context, new MaterialPageRoute(
                builder:  (BuildContext context) {
                 return  ProductDetailPage(data: newlist[index]);
                }
));