【问题标题】:Try to filter ListView.builder inside StreamBuilder in Flutter尝试在 Flutter 的 StreamBuilder 中过滤 ListView.builder
【发布时间】: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


【解决方案1】:

如果条件不正常,你就不是。

您为每个案例都返回 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.
....

【讨论】:

    【解决方案2】:

    假设您的 isFavorite 数据类型为 bool,如果它是字符串,则将以下 if 语句更改为此 snapshot.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);
                                    }
                                  );
        }
    

    【讨论】:

      猜你喜欢
      • 2019-08-19
      • 2023-02-11
      • 1970-01-01
      • 2021-10-12
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      相关资源
      最近更新 更多