英文:
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<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!);
);
},
),
答案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<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!);
},
);
},
);
},
);
},
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论