【发布时间】:2020-02-19 17:35:59
【问题描述】:
我是 Flutter 的新手,我正在尝试开发一个应用程序,所以 nom 在如何显示过滤后的 ListView.builder 时遇到了麻烦
这是小部件代码:
@override
Widget build(BuildContext context) {
print('Favorites');
return Container(
color: AppColors.hexToColor('F0F4F5'),
alignment: Alignment.topCenter,
child: Column(
children: <Widget>[
HeaderCity(),
Expanded(
child: Container(
padding: EdgeInsets.only(left: 16.0, right: 16.0),
child: Container(
child: StreamBuilder<CoworkingState>(
stream: Provider.coworkingBlocOf(context).streamCoworkingState,
initialData: CoworkingState(),
builder: (BuildContext context, AsyncSnapshot<CoworkingState> snapshot) {
return (snapshot.data.isLoading)
? CustomCircularLoader()
: ListView.builder(
padding: EdgeInsets.all(0.0),
itemBuilder: (BuildContext context, int index) {
if (snapshot.data.coworking.any((isFavorite) => true)){
return CoworkingCellDetail(
onTap: () {
Provider.coworkingBlocOf(context)
.selectCoworking(coworking: snapshot.data.coworking[index]);
Future.delayed(Duration.zero, () {
changeRoute(context, Routes.coworkDetail, false);
});
},
isFavorite: snapshot.data.coworking[index].isFavorite,
name: snapshot.data.coworking[index].name,
adress: snapshot.data.coworking[index].address,
phone: snapshot.data.coworking[index].phone,
hour: snapshot.data.coworking[index].openingHours,
urlImages: snapshot.data.coworking[index].imagesUrl,
onFavoriteTap: () {
Provider.coworkingBlocOf(context).removeFavorite(snapshot.data.coworking[index].id);
}
);
} return null;
},
itemCount: snapshot.data.coworking.length,
);
}),
),
),
),
],
),
);
}
所以现在返回一个完整的列表,我只想显示带有isFavorite = true的项目
【问题讨论】:
-
你真的需要流吗?可以使用 Future 来完成吗?无论如何...在通过流发送数据之前,您可以在列表中应用过滤器。 Dart 中的运算符称为“where”。
标签: flutter