flutter - 如何将 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 [''];
}
}
解决方案
在这种情况下,您可以使用 FutureBuilder 而不是 StreamBuilder。
推荐阅读
- reactjs - 我该如何解决这个问题 - 我尝试了所有方法但不起作用
- python - Python 3 在函数内部声明实例失败,需要位置参数:'self' 错误
- jmeter - 如何在 Jmeter 控制台中打印取自 csv 的数据
- java - 如果我想重载从超类继承的方法,什么是好的设计实践?
- c - double 变量显示为 double * 和 int 变量显示为 int * - 为什么?
- java - 在 ArrayList 中获取对象哈希码而不是对象值
- python-3.x - Python:使用理解将列表列表转换为字典
- python - 添加任何延迟时,Python selenium 脚本崩溃
- reactjs - 尝试从 fontawesome npm 迁移到自托管时未定义定义
- javascript - Javascript 日期结果控制台与 MySQL 结果不匹配