我是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
的项目
答案 0 :(得分:0)
如果条件无法正常工作,您就不会这样做。
对于每种情况,您都返回true,这就是为什么添加所有项目的原因。
尝试以下一项:
...
if (snapshot.data.coworking.any((isFavorite) => isFavorite)){ //here it return true if isFavorite is true and if isFavorite is false then it will return false.
....
答案 1 :(得分:0)
假设您的isFavorite数据类型为bool,如果它是字符串,则将以下if语句更改为此快照。data.coworking[index] .isFavorite.toString()==“ true”
if(snapshot.data.coworking[index].isFavorite){
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);
}
);
}