Flutter Cubit Listener Listview 数据在按下按钮时不刷新?

发布于 2025-01-19 01:07:23 字数 2366 浏览 6 评论 0原文

我有两个listViews是水平的,当我单击“水平列表视图”按钮以获取API的数据并在垂直列表视图中更新值时,另一个是垂直的。

这里正在发生API调用,但值未更新ListView和Cubit中未显示的进度栏。

我已经在侦听器Bool变量IS Lifate IS Loading中使用了默认情况下。当Tap上发生在水平列表视图IS Loading集合中时,请设置为true。

OnInitState我使用了

setState(() {
  isLoading;
  stypeunits;

});

集团消费者和听众代码

  BlocProvider.value(
  value: _unitBookingCubit,
  child: BlocConsumer<UnitBookingCubit,UnitBookingState>(
    builder: (context, state) {

      if (state is UnitBookingLoading) {
        return Loading();
      } else if (state is ErrorState) {
        return ErrorTxt(
          message: '${state.error}',
          ontap: () => _loadstoragetype(),
        );
      }

      if (state is FacilityUnitTypeload) {
        if (state.storageTypeModel.isSuccess!) {
          stypeunits=state.storageTypeUnits?.result!;
         return buildunitWidgets(storagetypelist:state.storageTypeModel.result);
        } else {
          return Container(
            height: double.infinity,
            child: Center(
                child: textStyle(
                  text:   state.storageTypeModel.returnMessage!,
                  style: TextStyles.subTitle2,
                )),
          );
        }
      }

      if (state is FacilityUnitsload) {
        print('Unitsloaded buil');
        if (state.storageTypeUnits.isSuccess!) {
          print('Unitsloaded1');
          stypeunits=state.storageTypeUnits.result;
          return buildunitWidgets();
        } else {
          return Container(
            height: double.infinity,
            child: Center(
                child: textStyle(
                  text:   state.storageTypeUnits.returnMessage!,
                  style: TextStyles.subTitle2,
                )),
          );
        }
      }


      return buildunitWidgets();
    },
    listener: (context, state) {
      if (state is UnitBookingLoading) {
        isLoading=true;
      }
      if (state is FacilityUnitsload) {
        print('Unitsloaded');
        if (state.storageTypeUnits.isSuccess!) {
          print('Unitsloaded1');
          stypeunits=state.storageTypeUnits.result!;
          print('Unitsloaded1${stypeunits?.length}');
          isLoading=false;
        }
      }


    },
    buildWhen: (context, state) {
      return state is FacilityUnitTypeload;
    },
  ),
)

I have two listviews one is horizontal the other is vertical when I click on the horizontal listview button to fetch the data from API and update the value in the vertical listview.

Here API call is happening but values not updating in listview and the progress bar not showing in a cubit.

I have used inside listener bool variable isloading by default false.when on tap is happened in horizontal listview isloading set as true.

oninitState i have used

setState(() {
  isLoading;
  stypeunits;

});

bloc consumer and listener code

  BlocProvider.value(
  value: _unitBookingCubit,
  child: BlocConsumer<UnitBookingCubit,UnitBookingState>(
    builder: (context, state) {

      if (state is UnitBookingLoading) {
        return Loading();
      } else if (state is ErrorState) {
        return ErrorTxt(
          message: '${state.error}',
          ontap: () => _loadstoragetype(),
        );
      }

      if (state is FacilityUnitTypeload) {
        if (state.storageTypeModel.isSuccess!) {
          stypeunits=state.storageTypeUnits?.result!;
         return buildunitWidgets(storagetypelist:state.storageTypeModel.result);
        } else {
          return Container(
            height: double.infinity,
            child: Center(
                child: textStyle(
                  text:   state.storageTypeModel.returnMessage!,
                  style: TextStyles.subTitle2,
                )),
          );
        }
      }

      if (state is FacilityUnitsload) {
        print('Unitsloaded buil');
        if (state.storageTypeUnits.isSuccess!) {
          print('Unitsloaded1');
          stypeunits=state.storageTypeUnits.result;
          return buildunitWidgets();
        } else {
          return Container(
            height: double.infinity,
            child: Center(
                child: textStyle(
                  text:   state.storageTypeUnits.returnMessage!,
                  style: TextStyles.subTitle2,
                )),
          );
        }
      }


      return buildunitWidgets();
    },
    listener: (context, state) {
      if (state is UnitBookingLoading) {
        isLoading=true;
      }
      if (state is FacilityUnitsload) {
        print('Unitsloaded');
        if (state.storageTypeUnits.isSuccess!) {
          print('Unitsloaded1');
          stypeunits=state.storageTypeUnits.result!;
          print('Unitsloaded1${stypeunits?.length}');
          isLoading=false;
        }
      }


    },
    buildWhen: (context, state) {
      return state is FacilityUnitTypeload;
    },
  ),
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文