Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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 如何在Futter循环中停止Futurebuilder_Flutter_Dart - Fatal编程技术网

Flutter 如何在Futter循环中停止Futurebuilder

Flutter 如何在Futter循环中停止Futurebuilder,flutter,dart,Flutter,Dart,一旦Futurebuilder停止返回芯片,我就很难弄清楚如何停止循环。 对于我未来的构建器,我会收到一个字符串列表,然后根据该字符串的索引创建一个芯片,然后将其放入我稍后在程序中使用的列表中 extractFutureChip(_response, index){ return FutureBuilder<String>( future: _response, // if you mean this method well return image u

一旦Futurebuilder停止返回芯片,我就很难弄清楚如何停止循环。 对于我未来的构建器,我会收到一个字符串列表,然后根据该字符串的索引创建一个芯片,然后将其放入我稍后在程序中使用的列表中

extractFutureChip(_response, index){

return FutureBuilder<String>(
              future: _response, // if you mean this method well return image url
              builder: (BuildContext context, AsyncSnapshot<String> snapshot) {

                if(snapshot.connectionState == ConnectionState.done){
                  var jsonString = snapshot.data;
                  //print(jsonString);
                  List terms = jsonDecode(jsonString);
                  if(index>=terms.length){
                    return Container(height: 0.0,width: 0.0,);
                  }
                  //print(terms);
                  Chip return_val = Chip(

                    backgroundColor: Colors.orange[900],
                    labelStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                    label: Text(terms[index])
                  );
                    return return_val;

                }else if(snapshot.connectionState == ConnectionState.waiting){
                  return Chip(

                    backgroundColor: Colors.grey[700],
                    labelStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                    label: Text("Loading")
                  );
                }
               return Chip(

                    backgroundColor: Colors.black,
                    labelStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                    label: Text("ERROR")
                  );
              }
            );
}
但这里的两个问题是,如果一个人有超过200件东西,
当大多数响应中的字符串少于5个时,搜索大量内容是非常低效的。

因此我认为您误解了FutureBuilder的目的:1 FutureBuilder只会在传入的将来更改时更新小部件树,2它意味着返回小部件树。因此,从_responseobj构建列表的最简单方法是在方法中使用小部件

例如

另一方面,如果您希望拆分异步请求以获取未来数据和小部件构建,那么您可能应该使用某种init函数而不是FutureBuilder:

Future<List<String>> someFunc() async {
  try {
    var jsonString = await _response;
    // do some more manipulation or other stuff
    return jsonDecode(jsonString);
  } on Exception catch (e) {
    // TODO you can catch exceptions coming from the Future here
  }
}

然后用返回的字符串列表构建芯片

有什么特别的原因让你一点一点地构建芯片,而不是在整个未来完成后构建整个芯片列表?我想你应该仔细阅读@Wecherowski有什么方法可以完成整个列表吗?我认为FutureBuilders只适用于一个widget您只需要FutureBuilder来展示您的未来数据-而且您只需要一个FutureBuilder而不需要多个FutureBuilders-为什么要在技能列表中存储多个FutureBuilders?最有可能的技能列表应该存储FutureBuilders,而不是FutureBuilders,比如:child:FutureBuilder/*import'package:english\u words/english\u words.dart'作为单词;var chips\u data=Future.delayedDurationseconds:4,=>words.nomes.take16;*/future:chips_data,builder:context,snapshot{if!snapshot.hasData return circularprograrpressindicator;var i=0;return Wrap space:8,Chip-label:Text'${i++}$n',backgroundColor:i.isOdd?Colors.black12:Colors.black26,elevation:4,], ; },
return FutureBuilder<String>(
  future: _response, // if you mean this method well return image url
  builder: (context, snapshot) {
    if(snapshot.connectionState == ConnectionState.done){
      var jsonString = snapshot.data;
      List terms = jsonDecode(jsonString);

      return Wrap(
        children: List.generate(terms.length, (index) => Chip(
            backgroundColor: Colors.orange[900],
            labelStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
            label: Text(terms[index])
        )),
      );
    } else if (...) { ... }
    else return Container(); 
  });
Future<List<String>> someFunc() async {
  try {
    var jsonString = await _response;
    // do some more manipulation or other stuff
    return jsonDecode(jsonString);
  } on Exception catch (e) {
    // TODO you can catch exceptions coming from the Future here
  }
}