我对流有问题。我从流中获取空值。我的目标是合并2个Future,因此当用户未搜索任何参数时我将使用第一个Future,而在用户搜索时将使用另一个Future,因为我有2个端点用于搜索列表,而另一个端点没有过滤器。我不确定这是否是应用程序开发人员的标准行业惯例。如果不是,请告诉我,我应该如何设置后端
Widget Body(BuildContext context, TabController taby) {
Future<List<littleOffers>> searchdata = null;
Stream<List<littleOffers>> _dataSwitch() async* {
if (searchdata == null)
yield* Stream.fromFuture(fetchOffers());
else
yield* Stream.fromFuture(searchdata);
}
void resetSearchData() {
searchdata = null;
}
void setSearchData(Future<List<littleOffers>> data) {
searchdata = data;
}
return Column(
children: <Widget>[
Search_Sort(context),
Expanded(
child: StreamBuilder(
stream: _dataSwitch(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.none || !snapshot.hasData) {
print('project snapshot data is: ${snapshot.data}');
print('project snapshot state is: ${snapshot.connectionState.toString()}');
return Container();
} else {
return TabBarView(controller: taby, children: [
getList(snapshot.data),
getList(snapshot.data),
getList(snapshot.data),
getList(snapshot.data),
]);
}
}),
),
],
);
}
最佳答案
那可能有效,但我认为您错过了异步并等待setSearchData
void setSearchData(Future<List<littleOffers>> newData) {
setState(()async{
searchdata = await newData;
});
}
并删除数据void clearSearchData() {
setState((){
searchdata = null;
});
}
关于flutter - 2个 future 一股波动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64624320/