I was migrating to newer version of bloc, but on the following code I am getting recursive API calls & reloads if i use "ctx.watch" instead of "ctx.bloc". I tried using "listenwhen" but that also didn't work.
BlocListener<HomeCubit, HomeState>(
// "ctx.watch<HomeCubit>..loadData()" is not working
cubit: ctx.bloc<HomeCubit>()..loadData(),
listener: (ctx, state) {
if (state is HomeLoaded) {
final username = (ctx.read<AuthBloc>().state as Authenticated)
.authenticatedUser
.name;
FlushbarHelper.createSuccess(message: "Welcome $username").show(ctx);
} else if (state is HomeFailed) {
FlushbarHelper.createError(message: state.failure.message).show(ctx);
}
},
Hi @San0330 馃憢
Thanks for opening an issue!
You can use BlocProvider.of<HomeCubit>(context)..loadData() but I would strongly avoid calling loadData in this case because each time the widget is rebuilt you will call loadData. You should call loadData in the initState of the widget instead and you can use context.read<HomeCubit>().loadData() there.
Let me know if that helps 馃憤
Yes it worked...Thanks !
I was refactoring my old code, so I forgot i could convert it to stateful widget.
Most helpful comment
Yes it worked...Thanks !
I was refactoring my old code, so I forgot i could convert it to stateful widget.