如何在使用Flutter中的Bloc从API中使用删除方法后刷新ListView Builder?

huangapple go评论59阅读模式
英文:

How to refresh listview builder after using delete method from api using Bloc in flutter?

问题

以下是翻译好的部分:

如何在使用Flutter中的Bloc删除API方法后刷新ListView.builder?在完成删除方法后,我希望数据从列表中消失,但它不会消失。我必须刷新页面才能让它消失。在Bloc Listener中,有一个名为success的状态。我应该如何在此状态下实现代码,以便在成功时它会消失?

ListView.builder(
  shrinkWrap: true,
  physics: const NeverScrollableScrollPhysics(),
  itemCount: _data.length,
  itemBuilder: (_, index) {
    return BlocListener<DeleteExperienceCubit, DeleteExperienceState>(
      listener: (context, state) {
        state.when(
          initial: () => const Containers(),
          loading: () => const CircularProgressIndicator(),
          noInternet: () => null,
          error: (message) => null,
          success: (data, message) {
            context.showSnackbarSuccess(message: message);
          },
        );
      },
      child: BlocBuilder<DeleteExperienceCubit, DeleteExperienceState>(
        builder: (context, state) {
          return ExperienceContainer(
            companyName: _data[index].company!.name!,
            from: _data[index].from!,
            deleteTap: () async {
              await context.read<DeleteExperienceCubit>().deleteRepository(
                id: _data[index].id!
              );
            },
          );
        },
      ),
    );
  },
);

请注意,上述代码是对您提供的代码的翻译部分,不包括任何额外的内容。如果您需要更多帮助或有其他问题,请随时提出。

英文:

How do I refresh the listview builder after using the delete method from the API using Bloc in Flutter? After the delete method is completed, I want that data to disappear from the list, but it won't. I have to refresh the page for it to disappear. In Bloc Listener, there is a state called success. How do I implement code in this state so that it will disappear when it is successful?

    ListView.builder(
    shrinkWrap: true,
    physics: const NeverScrollableScrollPhysics(),
    itemCount: _data.length,
    itemBuilder: (_, index) {
    return BlocListener&lt;DeleteExperienceCubit, DeleteExperienceState&gt;(
    listener: (context, state) {
    state.when(
    initial: () =&gt; const Containers(),
    loading: () =&gt;  const CircularProgressIndicator(),
    noInternet: () =&gt; null,
    error: (message) =&gt; null,
     success: (data, message) {context.showSnackbarSuccess (message: message);
                                                },
                                              );
                                            },
    child: BlocBuilder&lt;DeleteExperienceCubit,DeleteExperienceState&gt;(
     builder: (context, state) {return ExperienceContainer(
     companyName:  _data[index].company!.name!,
     from: _data[index].from!,
     deleteTap: () async {
     await context .read&lt;DeleteExperienceCubit&gt;().deleteRepository(
      id: _data[index].id!);
   );
   },
   ),

答案1

得分: 1

我们从声明一个 Listener 然后一个相同 Bloc 的 Builder 开始得不对。请使用 Consumer

此外,只有当你想要“监听”变化,比如 Snackbars、屏幕间导航,而不是改变你的小部件时,才留下 Listener

    return ListView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: _data.length,
      itemBuilder: (_, index) {
        return BlocConsumer<DeleteExperienceCubit, DeleteExperienceState>(
          listener: (context, state) {
            state.when(
              success: (data, message) {
                context.showSnackbarSuccess(message: message);
              },
            );
          },
          builder: (context, state) {
            state.when(
              initial: () => Container(),
              loading: () => const CircularProgressIndicator(),
              noInternet: () => null,
              error: (message) => null,
              success: (data, message) {
                return ExperienceContainer(
                  companyName: _data[index].company!.name!,
                  from: _data[index].from!,
                  deleteTap: () async {
                    await context
                        .read<DeleteExperienceCubit>()
                        .deleteRepository(id: _data[index].id!);
                  },
                );
              },
            );
          },
        );
      },
    );
英文:

We started wrong since you declare a Listener and then a Builder of the same Bloc. Use the Consumer.

Apart, leave the Listener only if you want to "listen" for changes like Snackbars, Navigate between screens, not to change your widgets.

    return ListView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: _data.length,
      itemBuilder: (_, index) {
        return BlocConsumer&lt;DeleteExperienceCubit, DeleteExperienceState&gt;(
          listener: (context, state) {
            state.when(
              success: (data, message) {
                context.showSnackbarSuccess(message: message);
              },
            );
          },
          builder: (context, state) {
            state.when(
              initial: () =&gt; Container(),
              loading: () =&gt; const CircularProgressIndicator(),
              noInternet: () =&gt; null,
              error: (message) =&gt; null,
              success: (data, message) {
                return ExperienceContainer(
                  companyName: _data[index].company!.name!,
                  from: _data[index].from!,
                  deleteTap: () async {
                    await context
                        .read&lt;DeleteExperienceCubit&gt;()
                        .deleteRepository(id: _data[index].id!);
                  },
                );
              },
            );
          },
        );
      },
    );

huangapple
  • 本文由 发表于 2023年6月8日 14:41:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定