Hello,
How can I get latest changes when I am dispatching an event through another event, currently I am not getting reactive data, my widget is not rebuilding too
For example, lets say I have two blocs, otpBloc and userBloc, inside otpBloc I am sending http request to backend server with 4 digit OTP, server check that OTP and if all Ok it send token and user as response, now I have to store token offline and also update user state, so creat userBloc reference in otpBloc, and dispatch uaerBloc.setAuth and pass token and user to even, in userBloc I am just storing token using shared preference and also updating user, now real problem is I don't get this reactive user data if I wrap my widget inside userBoc, but instead of dispatching setAuth through otpBloc, if I direct dispatch setAuth it works
Hi @kunaldodiya 馃憢
Thanks for opening an issue!
Can you please share a sample app that demonstrates the problem you鈥檙e facing? It would be much easier for me to help if I鈥檓 able to reproduce your issue locally.
Thanks!
Hi @kunaldodiya 馃憢
Thanks for opening an issue!Can you please share a sample app that demonstrates the problem you鈥檙e facing? It would be much easier for me to help if I鈥檓 able to reproduce your issue locally.
Thanks!
Yeah sure, just a moment
@felangel
Hi, as mentioned providing demo link to test:- https://github.com/kunaldodiya/flutter-example
@kunaldodiya thanks! I'll take a look in the morning 馃憤
@kunaldodiya sorry for the delay but I took a look and the issue is you have two UserBloc instances. One is created in the OtpBloc and one is created in _MyHomePageState. BlocBuilder is listening for changes in the instance created in _MyHomePageState but the events are being dispatched to the one created in OtpBloc.
You need to make sure the events are dispatched to the same instance that you are subscribed to. I would recommend injecting the UserBloc into the OtpBloc via the constructor and that should resolve your issue.
class OtpBloc extends Bloc<OtpEvent, OtpState> {
final UserBloc userBloc;
OtpBloc({this.userBloc});
void verifyOtp(mobile, otp) {
dispatch(VerifyOtp(mobile: mobile, otp: otp));
}
...
}
Then in main.dart you can inject the instance
final UserBloc userBloc = UserBloc();
runApp(
BlocProviderTree(
blocProviders: [
BlocProvider<OtpBloc>(bloc: OtpBloc(userBloc: userBloc)),
BlocProvider<UserBloc>(bloc: userBloc),
],
child: MyApp(),
),
);
Hope that helps! 馃憤
Thanks