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 如何将未来转化为ListView的流_Flutter_Dart - Fatal编程技术网

Flutter 如何将未来转化为ListView的流

Flutter 如何将未来转化为ListView的流,flutter,dart,Flutter,Dart,我正在使用这个AutoCompleteTextField 如示例所示,它使用URL调用Future来提取数据,从而填充下拉式typeahead结果 我还需要在另一个ListView中使用返回的数据,而不仅仅是autocompletetextfield的下拉列表 我的问题是如何将FuturegetLocationSuggestionsList转换为流,以便在ListView中使用StreamBuilder Future<List<String>> getLocationS

我正在使用这个AutoCompleteTextField

如示例所示,它使用URL调用Future来提取数据,从而填充下拉式typeahead结果

我还需要在另一个ListView中使用返回的数据,而不仅仅是autocompletetextfield的下拉列表

我的问题是如何将Future
getLocationSuggestionsList
转换为流,以便在ListView中使用StreamBuilder

Future<List<String>> getLocationSuggestionsList(String locationText) async {
    final bloc = BlocProvider.of<EditProfileBloc>(context);
    List<String> suggestionList = List();
    LocationModel data = await bloc.fetchLocationSuggestions(locationText);
    if (data != null) {
      for (Predictions predictions in data.predictions) {
        suggestionsKeyValuePairs[predictions.description] = predictions.placeId;
        if (!suggestionList.contains(predictions.description))
          suggestionList.add(predictions.description);
      }
      return suggestionList;
    } else {
      return [''];
    }
  }
Future GetLocationSuggestionList(字符串locationText)异步{
最终集团=BlocProvider.of(上下文);
列表建议列表=列表();
LocationModel数据=wait bloc.fetchLocationSuggestions(locationText);
如果(数据!=null){
用于(数据中的预测。预测){
SuggestionKeyValuePairs[predictions.description]=predictions.placeId;
如果(!suggestionList.contains(predictions.description))
suggestionList.add(预测.描述);
}
返回建议列表;
}否则{
返回[''];
}
}

在这种情况下,您可以使用FutureBuilder而不是StreamBuilder。

期货是一次性申请的,对吗?每当用户停止键入时,就会调用此函数。所以我想,既然每次停止时都会调用该方法,我可以使用FutureBuilder?我明白了。您可以使用FutureBuilder和setState方法来完成,但这会有点混乱。通过对未来使用asStream()方法,可以将未来变成流。