I am new to Flutter development and using Flutter Bloc, but one thing I don't understand real well is how to handle application Logout and API Authentication failure. There are two situations:
I was following the Flutter Login tutorial but I am not clear on how this would be best implemented in a larger application.
I have an auth Bloc like the tutorial but it seems that in order to trigger the state change you need to pop navigation off the stack each time, seems a bit cumbersome.
Hello @mflesh1,
Giving a few code examples as to what you're trying to achieve, and why you think it isn't helping you will enable the community to help better.
I don't understand what you mean by
in order to trigger the state change you need to pop navigation off the stack
Because you need not pop navigation off the stack to trigger a state change.
If this could help, you need to add an event to the Bloc once a Data Provider/Repository detects authentication failure, which triggers a state change in the Bloc. Use a BlocListener to listen for this state change somewhere at a higher level of the widget tree, and in listener function use Navigator.pushNamed or Navigator.popUntil to get to your desired screen.
Hi @mflesh1 馃憢
Thanks for opening an issue!
As @theDarkBoffin mentioned, it would help if you could provide a link to a sample app which illustrates the problem you're having. Additionally, if you haven't already I'd recommend joining the discord and we can also do a live coding session 馃憤
I have checked in project go github.
https://github.com/mflesh1/flutter_bloc_security
The initial code in question is in the listener in the login_form.dart file.
listener: (context, state) {
if (state is LoginFailure) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('${state.message}'),
backgroundColor: Colors.red,
),
);
} else if (state is LoginLoaded) {
Navigator.of(context).pop(context);
BlocProvider.of<AuthBloc>(context).add(new AuthorizedEvent(state.user));
}
}
The Navigator.pop is required to remove the login page from the view, but what happens if the navigation is deeper into the application after the user logs in, is it still required to pop the stack?
@mflesh1, thanks for the snippet! Now that I can clearly understand what you mean to say,
you can do either of these things:
Navigator.pushReplacementNamed, or Navigator.popUntil(context, ModalRoute.withName('/login')) to reach the login(or desired) route.I hope that helps.
@theDarkBoffin thanks for the reply. Is there any way to clear the stack and just load a screen?
I come from Android where I can just call an Activity and wipe the stack, anything similar in Flutter?
@mflesh1 Navigator.pushNamedAndRemoveUntil(context, '/login', (_) => false); might be what you're looking for.
@theDarkBoffin will this still work even if the '/login' is not in the current stack?
@mflesh1, yes.
@mflesh1 can we close this issue?
@theDarkBoffin thanks for taking the time to provide answers, I really appreciate it 馃憤
Closing for now 馃憤
Most helpful comment
@mflesh1 can we close this issue?
@theDarkBoffin thanks for taking the time to provide answers, I really appreciate it 馃憤