首页 > 解决方案 > 如何将 Future 变成 ListView 的流

问题描述

我正在使用这个 AutoCompleteTextField

https://github.com/sharansingh00002/Auto-Complete-TextField-Flutter

如示例中所示,它调用带有 URL 的 Future 来提取数据,该数据填充下拉式预先输入结果。

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

我的问题是如何将 FuturegetLocationSuggestionsList变成 Stream,以便我可以将 StreamBuilder 用于 ListView?

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 [''];
    }
  }

标签: flutterdart

解决方案


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


推荐阅读